>>> john = AddrBookEntry('John Doe', '408-555-1212') Created instance for: John Doe >>> jane = AddrBookEntry('Jane Doe', '650-555-1212') Created instance for: Jane Doe
Class MyData(object):
pass
>>> class MyData(object):
... pass
...
>>> mathObj = MyData()
>>> mathObj.x = 4
>>> mathObj.y = 5
>>> mathObj.x + mathObj.y
9
"x" and "y" belong to the mathObj, the mathObj act as a container.
Not of class MyData
self is like the C++ this key word.
this is the python philosophy, everything declared explicitly
__init__() is like the constructor
Python creates the instance for you and then calls __init__() during instantiation
to define additional behavior that should occur when a class is instanciated.
classAddrBookEntry(object): # class definition
'address book entry class'
def __init__(self, nm, ph): # define constructor
self.name = nm # set name
self.phone = ph # set phone#
print'Created instance for:', self.name
def updatePhone(self, newph): # define method
self.phone = newph
print'Updated phone# for:', self.nam
The only initialize the parameter is the name and phone that is --nm --ph