ࡱ> JLI` jbjb $2|c|cg8@@@@@@@,$,$,$8d$ p$,y*$$$$$$$$))))))),s+R-*@$$$$$*)@@$$3*)))$@$@$))TD,@@@@$)))@@)$ Y,$&@))I*0y*).).))@dClasses, Initialization, and Equality Structure of a class def class # assign data members # methods (functions) # constructor and other bookkeeping methods # computational methods Let's take a look at a sample class, Time. See (cs.usfca.edu/~wolber/courses/110/python/time0.py) class Time: hour=0 minute=0 second=0 # main t = Time() # create an instance of time t.hour=18 t.minute=3 t.second=5 print t.hour print t.minute print t.second Teacher Demonstration 1. add a function outside the class Time called 'isNight', with a parameter of type Time and a call in main to it. 2. Rewrite the function as a method subordinate to the class Time. Object Initialization (Constructors) When we create an instance of a class, we often want to initialize its data members (fields). We could create an instance of time set to12:30:30 with the following code in main: # main t = Time() hour=12 minute=30 second=30 This is quite laborious. A constructor is a special method for initializing fields on creation. In Python, a constructor is a method with the special name __init__ (two underscores around "init") Using a constructor, the code in the sample main can be reduced to one line. Here's a modified class Time and modified main: class Time: def __init__(self, hours, minutes, seconds): self.hours = hours self.minutes = minutes self.seconds = seconds # main t = Time(12, 30, 30) Object Equality Consider the following: # main t1 = Time(4,5,22) t2= Time(4,5,22) if (t1==t2) print "t1 is same as t2" Will anything be printed? With objects, "==" is defined to compare the addresses of the two objects. A programmer must define a method to compare the content of two objects. Python allows a programmer to redefine == for a class by using the special name __eq__. class Time: def __init__(self, hours, minutes, seconds): self.hours = hours self.minutes = minutes self.seconds = seconds def __eq__(self, other): if ((self.hours==other.hours) and (self.minutes==other.minutes) and (self.seconds=other.seconds)): return true else return false; Given this __eq__, we'll get a different result for: # main t1 = Time(4,5,22) t2= Time(4,5,22) if (t1==t2) print "t1 is same as t2" String Representation When we call print on an object, Python does not print the contents of the object. A programmer can write a method with the name __str__ to provide a string representation for an object (which can be printed) class Time: def __init__(self, hours, minutes, seconds): self.hours = hours self.minutes = minutes self.seconds = seconds def __eq__(self, other): if ((self.hours==other.hours) and (self.minutes==other.minutes) and (self.seconds=other.seconds)): return true else return false def __str__(self): return str (self.hours) +":"+ str(self.minutes) + ":" +str(self.seconds) In-Class Problems 1. Copy your Coordinate code into another file, then modify it so that it has a constructor (__init__) which allows the x and y coordinates to be set in the creation statement. Modify the main program so that it makes use of the constructor. 2. Write a __eq__ and __str__ method for Coordinate. Add code in the main that shows that these methods work. CS 110: Introduction to Computer Science Spring 2008 %&;# 1 B %:fh!h2:hB*CJOJQJph h2:h &;<QRpqPQabqgd$a$gdg./EF# $   { | . / _ ` gd` p q  1 B C [ \ j = > %gd%&Eh 4Pa%; MgdgdMNiGctggd^gdgd":p/ =!"#$%<@< NormalCJaJmH sH tH T@T #t Heading 1$<@&5CJ KH OJQJaJ R@R #t Heading 2$<@&56CJOJQJaJDA@D Default Paragraph FontRi@R  Table Normal4 l4a (k@(No List4@4 #tHeader  !4 @4 #tFooter  !2 z z z z2% &&;<QRpqPQabq./EF#$ {|./_`pq  1BC[\j=>%&Eh 4 P a  % ;    M N i  G c t   g0ʀ0ʀ00ʀ00ʀ00ʀ000000ʀ0000000000000000000000ʀ0ʀ000ʀ00000000000000000000000ʀ000ʀ0ʀ000000000000ʀ0000ʀ00000ʀ00000ʀ0ʀ0ʀ000000000ʀ0ʀ00000ʀ00ʀ00ʀ0ʀ0ʀ00ʀ00ʀ0ʀ0ʀ0ʀ0ʀ0ʀ00ʀ000ʀ00000ɀ 0@ ;<QRpqPQabq./EF#$ {|./_`pq  1BC[\j=>%&Eh 4 P a  % ;    M N i  G c t    zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw0 zw zw@ *@@ *@77777:` %M%-w~2<Q]t  " # 0 V ` q }   ) 5 6 C g@C\b{UZjny $ILdiy|eirt2<Q]t " D J \ ` q w    # V ` q }  ) 5 W ] o s k  g::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::@ hSS P @UnknownGTimes New Roman5Symbol3 ArialC Lucida Grande qh@+fH+f !24d@XPMacintosh HD:Applications:Microsoft Office 2004:Templates:My Templates:cs110.dotLinux TutorialUSF ITSUSF ITS Oh+'0h  , 8 D P\dlt|'Linux TutorialXUSF ITSSF  cs110.dotriUSF ITS1F Microsoft Word 11.1@0@@S@H@x GPICTd ,, MSWD ,Times New Roman 2.(w CS 110: I- Q)n-\)t-) r-)o-)duc-)Ht-`)i-)o- Q)n -)&t-\)o- Q) -) C-)!o-`)m-)'pu-)2t-)e-)r-) Sc-`)?i-)e- Q)n-)ce!-  ), -). (Spr-`)Fi- Q)n-)g-) -) 2008-\)d ,Arial B-(Classe->)s-)&, I->)7n-))i->)t-)ia->)7l-)iza->)Xt-)i->)on-)R,-@) ->)an-)Od E->)gq-))u->)(a-)&lit):y 6-)% ,Arial :-(wStru-F)tc-)!ture-F)m o-)5f-) -)a- )  -)cl-F)0a-)!s) s-)   6-(`w -*@ )Dd)"ef )C class -( (w -*@ # )assign ) data memb- K( e-)rs-)2 (_w -*@ #- K) -)methods) (function-)s- K))-`) -(w -*@ - K) -)#-)" -) constructor- K( -)and-)b -)other- K) -)book- K)k-)!eeping-) -)methods-) (_w -*@ - K) -)#-)" -) computational- K( -)m- K)2e-)thods-) (w -*@&Let's take a look at a sample class, T-(7i-)me)P.) )S)e- K)e -(^w(cs.-)[u-)!sfca.edu/~wolber/courses/110/- K(^Dp-)# ython/time0.- K(^p-)#y- K))-) (w -*@ class Time:-( (w -*@ hour=0-8(] -(w minute=0-8( -(w second=0-8( -(w -*? # main-8) -(w t = Ti- K)m-)3e(- K)0)-) #-)3 -)create- K) -)an-)@ -)instance- K) -)of-)5 -)t-)i-)me-8)P -(w t.hour=18-8( -(w t.minute=3-8( -(\w t.secon(\yd=5-8)o -(w print t.hour-( -(w print t.minute-8(' -( w print t.second-( * ( [w -*@ Teacher De- K( m-)3 onstration-(  ( w -*@ 1. add a function outside the cl-( a-)ss- K)8 -)T-)"i-)me- K)P -)called-) -) 'isNight',- K) -)with-)p -)a ( Zw parameter- K( Z -)of-)5 -)typ- K)Re-) Time-) -)and- K)b -)a-) -)call- K)Z -)in-)2 -)main- K) -)to-)5 -)it.-)5 ( w -*@B2. Rewrite the function as a method subordinate to the class Time.-( P ( w  ! ! ! !  ! ! !  ! ! !  ! ! !  ! ! !  ! ! !  ! ! !  ! ! !  ! ! ! ՜.+,0 `hpx  ' Linux Tutorial&Classes, Initialization, and Equality Structure of a class) Object Initialization (Constructors) Object Equality String Representation In-Class Problems Title Headings  !"#$%&'()*+,-./013456789:;<=>?@BCDEFGHKRoot Entry FRj~M1Table.WordDocument$2SummaryInformation(2DocumentSummaryInformation8ACompObjXObjectPoolRj~Rj~ FMicrosoft Word DocumentNB6WWord.Document.8