OOP in Python
D-Man
dsh8290 at rit.edu
Wed Jun 20 14:15:02 EDT 2001
On Wed, Jun 20, 2001 at 10:54:50AM -0700, Jonas Bengtsson wrote:
| Ok I admit - I am a Python-newbie!
|
| What about local variables and class variables when dealing with
| classes?
| With local vars I mean temporaryly used variables in a function. Will
| they be treated as 'normal' instance attributes? Do I have to delete
| them by myself when I'm leaving a function?
| With class vars I mean if many class instances of a class may share a
| variable. In C++ this is accomplished by a static variable. How do I
| do in Python?
class Demo :
# this is a class variable, ("static")
var1 = "Hello"
# this function/method is called immediately after construction.
# It is supposed to inialize the instance, referred to by the
# first argument.
def __init__( self ) :
# this creates an instance variable
self.var2 = "World"
def meth1( self ) :
# this is a local variable. It is not prefixed by 'self.' and
# will be destroyed upon completion of the function
var3 = "."
# one of the following lines accesses the class variable
# through the class object, the other through the instance
# object
print Demo.var1 + " " + self.var2 + var3
print self.var1 + " " + self.var2 + var3
demo_obj = Demo()
demo_obj.meth1()
HTH,
-D
More information about the Python-list
mailing list