help on object programing

DouhetSukd at gmail.com DouhetSukd at gmail.com
Fri Aug 17 16:32:58 EDT 2007


> how can i define my variables so that there are valid outside the
> class???

Not to be obnoxious, but your sample code has a number of fairly big
conceptual issues (like subclassing self and assigning 32 at the big
class level and printing 'list' which is a built-in type).  Been there
myself - it took me a while to understand class vs. instance
variables.  You are also using fairly advanced techniques such as
embedded classes which look above a newbie level.

I took the liberty to comment and "fix" up your code a bit so that it
runs:

#classes either inherit or not and signify that in parenthesis.
inheriting 'self' makes no sense
#for a class declaration.  but it does make perfect sense to have
'self' as a param to a class method (def)

class big:

    #class-level variable
    x = 32

    #changed to mylist to avoid confusion with list built-in.
    mylist = []

    #this is a normal instance method - 'self' refers to the class
instance you just created.
    def inside (self):

        #is this really what you wanted?  an embedded class -
syntaxically correct, but not used often
        class small: # a new class defined inside the first

            y = 348
            #ok, now I am referring to the mylist variable associated
(bound in python-speak) to the big class.
            #y is defined here so no need to do anything
            big.mylist.append(y) # send the value to first list
            #same with big.x
            big.mylist.append(big.x)


#instantiate the class, because you are calling an instance method
(i.e. you need to have created an instance to use that method)
#call the call
mybig = big().inside()

#refer to the mylist variable declared at the class, not instance
level.
#class level means any other calls you make will alter that variable
print 'my class level mylist variable:',big.mylist

console output:

my class level mylist variable: [348, 32]

Can you perhaps rephrase your requirements to indicate what you want
to achieve?

Strictly speaking, it looks like you could do this:

class Big:
    def getlist(self):
       return [348,32]

mylist =Big().getlist()

That's probably not what you were asking for, but it does express the
results you would get out of your code, especially as you are not
passing in any significant parameters to the 'inside' function.

OK, perhaps a bit more useful.

#no inheritance - could also be class Big(object) where object is the
python "root class"
class Big:

   #initialize the class level to be empty
   myclasslist = []
   def __init__(self):
      #initialize the instance level variable to be empty
      self.myinstancelist = []

   def appendAndGet(self,x,y):
      #modify the instance's "personal" list
      self.myinstancelist.append(x)
      self.myinstancelist.append(y)
      #will now modify shared class-level variable.
      Big.myclasslist.append(x)
      Big.myclasslist.append(y)
      return self.myinstancelist


print "Big.myclasslist without any instances around:", Big.myclasslist


bigA = Big()
result = bigA.appendAndGet(348,32)
print "result #1:", result
print "Big.myclasslist:", Big.myclasslist

bigB = Big()
result = bigB.appendAndGet(11,22)
print "result #2:", result

#and the instance also still has its myinstancelist around
print "same as result #2:", bigB.myinstancelist

print "Big.myclasslist:", Big.myclasslist

console output:

D:\user\workspace\vg\tmptesting>testc.py
my class level mylist variable: [348, 32]
Big.myclasslist without any instances around: []
result #1: [348, 32]
Big.myclasslist: [348, 32]
result #2: [11, 22]
same as result #2: [11, 22]
Big.myclasslist: [348, 32, 11, 22]

Try perhaps Dive Into Python's class intro:

www.diveintopython.org/object_oriented_framework/defining_classes.html

Cheers





More information about the Python-list mailing list