# define function to pause in tutorial def pause(): raw_input('...') print '\n'*24 # class definition print "# basic class definition" print '>>> class X:\n e = 12 # class variable\n def m(x): # method m\n print "--> argument to method m is", x' class X: e = 12 def m(x): print "--> argument to method m is", x pause() print "# print class variable and method (just to see what they are)" print "print X.e, X.m" print X.e, X.m pause() print "# create an instance of class X" print "a = X()" a = X() pause() print "# look at the instance variables" print "print a.e, a.m" print a.e, a.m pause() print "# write over the instance variable e" print "a.e += 1" a.e += 1 print "print a.e, X.e" print a.e, X.e pause() print "# perhaps unexpected ... a.e is really an instance variable!" print "# we can even add new instance variables on-the-fly" print "# (these are called object attributes)" print "a.q = [0,True]" print "print a.q" a.q=[0,True] print a.q pause() print "try invoking the method of a" print "a.m(100)" print '''Traceback (most recent call last): File "C:\Documents and Settings\Ted Herman\Desktop\tutClass.py", line 41, in -toplevel- a.m(100) TypeError: m() takes exactly 1 argument (2 given)''' print "# does Python have a bug?! how can m(100) have two given arguments?" pause() print "# try the method of a with no arguments" print "a.m()" a.m() pause() print '# solution to riddle: every method of a class is passed a "secret", first' print '# argument: it is the "self" parameter, the object handle' print '# so we rewrite X to expect this' print '>>> class X:\n e = 12 # class variable\n def m(self,x): # method m\n print "--> argument to method m is", x' class X: e = 12 def m(self,x): print "--> argument to method m is", x print "a = X() # let a become an instance of class X" a = X() print "# now try a.m(100)" print "am.(100)" a.m(100) print "# that's better!" pause() print "# let's look at an example where a method uses instance variables" pause() print '''class Y: rememb = [] def deq(self): if len(self.rememb)>0: r = self.rememb[0] self.rememb = self.rememb[1:] return r else: return None def enq(self,item): self.rememb.append(item)''' class Y: rememb = [] def deq(self): if len(self.rememb)>0: r = self.rememb[0] self.rememb = self.rememb[1:] return r else: return None def enq(self,item): self.rememb.append(item) print "# create an instance of Y" print "b = Y()" print "print b.deq()" b = Y() print b.deq() pause() print "# demonstrate methods of object b" print "b.enq(99)" print "b.enq(100)" b.enq(99) b.enq(100) print "# let's spy on b, looking at its rememb attribute" print "print b.rememb" print b.rememb print "# print some deq() applications of b" print "for i in range(0,3): print b.deq()" for i in range(0,3): print b.deq() pause() print "# what if we want to construct an object with some initialization parameters?" print "# solution: define a constructor method (called __init__)" print '''class Z: def __init__(self,x,y): self.coord = [x,y] def xpos(self): return self.coord[0]''' class Z: def __init__(self,x,y): self.coord = [x,y] def xpos(self): return self.coord[0] print "v = Z(40,200)" v = Z(40,200) print "print v.xpos()" print v.xpos() pause() print "# create some instances of Z" print "z1 = Z(2,6)" print "z2 = Z(2,6)" print "z3 = z2" z1 = Z(2,6) z2 = Z(2,6) z3 = z2 print "# now test if they are the same ..." print "z2 == z3" print z2 == z3 print "z1 == z2" print z1 == z2 print "# is this a bug?" pause() print "# answer: z2 and z3 refer to the same object, so they are equal;" print "# whether z1 and z2 are equal depends on your point of view," print "# but Python does allow us to extend operators ==, >, <, etc for" print "# whatever kind of object we like, for instance ..." print '''class Q: def __init__(self,x,y): self.coord = [x,y] def __eq__(self,other): return self.coord == other.coord def xpos(self): return coord[0]''' class Q: def __init__(self,x,y): self.coord = [x,y] def __eq__(self,other): return self.coord == other.coord def xpos(self): return self.coord[0] print "# now create instances and compare" print "w1 = Q(2,6)" print "w2 = Q(2,6)" print "w3 = Q(2,7)" w1 = Q(2,6) w2 = Q(2,6) w3 = Q(2,7) print "w1 == w2" print w1 == w2 print "w2 == w3" print w2 == w3 pause() print "# not covered: how to use inheritance, multiple inheritance in defining classes" print "# also not covered: how to define new types of exception (using classes)"