Confusion About Classes

M.E.Farmer mefjr75 at hotmail.com
Thu Dec 30 01:42:05 EST 2004


Thanks for the followup,
I just wanted to mention that you still need to read that stuff thru
several times, because...
there are no variables in python (yea I know others will say otherwise
;)
Yep, I said it, none.
There are NAMES, thats why there are NAMESPACES.
A name is just a reference or 'handle' to an object, because.....
everything in python is an OBJECT,(except for names, they are just
names).
And before you go and wipe out all those double underscores note that
python uses double underscores as special methods and other internal
details( read the docs).
A quick example is :( yeah it's deja vu)
py>class bla:
...    def __init__(self):
...        self.data=0
...    def setData(self, data):
...        self.data = data
...    def getData(self):
...        return self.data
...    def nullData(self):
...        self.setData(None)
Not all underscores method names are unnecessary.
__init__ is a special method that is called by python when we create an
instance of this class.  Most of the double underscore methods are
special  methods and are part of python( later you will see they can be
used for object customization )
Now back to self... self is reference to.. well self. Python is
explicit . A class has it's own namespace so we pass a referance around
to all it's member functions. when we set an attribute of a class it
can be done inside or out . Self.data is an example of setting a
attribute of an object from inside the class ( notice we referance
ourselves inside the class , because we are assigning the name to that
namespace.)
An example of settng attrbutes outside a class:
bl = bla()
bl.MyNewData = [1,2,3,4,5]
dir(bl)

As a matter of fact a common use of a class is like a container to
store data.
class null:
pass
a =null()
a.name = 'mike'
a.weight= 220
dir(a)
del  a.name
Sorry if it is incoherent, I am way past bed time .
M.E.Farmer




More information about the Python-list mailing list