Friday, March 23, 2012

csci133class.py

Tkinter module provide many data types, such as Frames, Labels, and Buttons. They equipped with their own sub routines, get for Entry, some of them is standard, like pack, and after. But wouldn't it be nice if we can do their to all the data type (class) we create?

For our ice cream store, we want to create memebership account (Object).
standardMember = Account('George Chan')
standardMember.deposit(100)
Or, for a worker timesheet program, we can create worker that contain other datatype object)
class iceCreamMember:
def __init__(self, name, age):
    self.myName = name
    self.myAge = age
def getAge(self):
    return self.myAge
def getName(self):
    return self.myName

member1 = iceCreamMember('George', 23)
member2 = iceCreamMember('Gerry', 29)

print(member1.getAge())
print(member2.getName())
In python, when we want to create a new datatype (new class), we would use a class statement. The keyword class tells python that it is a new datatype we are creating, notice it is a good style to always name your class with a upper case letter.

And when we want to create an instance of the iceCreamMemeber, an individual object of this new type, we will use the class name as it is a function.
member1 = iceCreamMember('George', 23)
This create a new iceCreamMember object, and member1 refers to it. Remember the constructor is called when we create an instance of the object in C++? In python, the __init__ function will be called right away.
def __init__(self, name, age):
Look at this line, the __init__ function takes 3 parameters, but look at the line #20, we only actually passing 2 to it. Why? Because the first argument is always set to refer to the new instance we just created. (Self, itself). You can name it anything, but the first one is always point to the object itself, so it makes senss to use 'self'. The order of ('George', 23) is important, because name gets 'George', as a string, and age gets 23, as an integer.

Different from C++, python's class instance does not have member variable pre say. They have attributes, and you do it with the syntax of.
self.myName = name
self.myAge = age
To create a function for our userdefined class, we just use def like we always do, with the difference of giving it a (self) parameter. Notice, when we call the function .getName(), we don't have to give it anything. Since the self argument is generated automatically.

But if we try to print(member1), something weird will happen, try it. It actually let us learn another fact about how python work.
>>> 
<__main__.iceCreamMember object at 0x0000000002EE1E48>
The reason why we get the memory address like output, it is because we have yet to "teach" python how we want to print this. python is computer, and that's what the iceCreamMemeber to python is at this moment, let's add another method to our class.
def __str__(self):
    return self.myName + ', and age ' + str(self.myAge)
So when we ask python to print, it will know what to print.

*Important* : self vs. Deck
class goldMemeber(iceCreamMemeber):
def __init__(self, discount):
    iceCreamMemeber.__init__(self)
    self.myDiscount = discount
def __str__(self):
    return "{0}% of discount for member".format(self.myDiscount)
We can understand this new class as: GoldMemeber is a kind of iceCreamMemeber. And the GoldMemeber inherits all the function the iceCreamMemeber has. So the getName, getAge function, will be provided to the GoldMemeber automically.
iceCreamMemeber.__init__(self)
*Important*: We must use the explicitly passing method to to pass the instead. If we use self.__init__(), we will be instead calling the goldMember.__init__ function. Which is the function we are trying to define right now at this moment.

No comments:

Post a Comment