0% found this document useful (0 votes)
14 views

module 5

lecture notes

Uploaded by

divyaushas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14 views

module 5

lecture notes

Uploaded by

divyaushas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 17
a Jochtin pushin wsho See 05 CG: # dates and dul] $ dss ond RO We have used many of Python's built-in types; now we are going to define a new type. As an example, we will create a type called Point that represents a point in two-dimensional space. In mathematical notation, points are often written in parentheses with a comma separating the coordinates For example, (0, 0) represents the origin, and (x, ¥) represents the point x units to the right and y ‘units up from the origin. There are several ways we might represent points in Python: 1. We cowld store the coordinates separately in two variables, x and y 2. We could store the coordinates as elements in a list or tuple 3. We could create a new type to represent points as objects, Creating a new type is more complicated than the other options, but it has advantages that will bbe apparent soon A programmer-defined type is also called a class. A class definition looks fike this, class Point: "Represents a point in 2-D space Post we +f, a0 vag Figure 15.1. Object diagram The header indicates that the new class is called Point. The body is a docstring that ex-plains what the class is for. You can define variables and methods inside a class definition, but we will get back to that later. Defining a class named Point creates a class object. Because Point is defined at the top level, its “full name” is _main__-Point. The class object is like a factory for creating objects. To create a Point, you call Point as if it were a function. blank = Point() blank | <__main__Point object at Oxb7e9d3ac> The return value is a reference to a Point object, which we assign to blank. Creating a new object is called instantiation, and the object is an instance of the class When you print an instance, Python tells you what class it belongs to and where it is stored in memory (the prefix Ox means that the following number is in hexadecimal) 2. Attributes > You can assign values to an instance using dot notation: blank. =3.0 blanky = 4.0 7 This syntax is similar to the syntax for selecting a variable from 2 module, such as mat pi or string, whitespace . 7 Intthis case, though, we are assigning values to named elements of an object. These elements are ‘called attributes. > A state diagram that shows an object and its attributes is called an object diagram: see Figure Is. 7 The variable blank refers toa Point object, which contains two attributes. Each attribute refers to 2 floating-point number. 7 You can read the value of an attribute using the same syntax: blanky | x = blank x 140 ix > The expression biank.x means, “Go to the object blank refers to and get the value of x~ In the example, we assign that value to a variable named x. There is no conflict between the variable x and the attribute x. > You can use dot notation as part of any expression. For example: | (Cog, %gy % (blank x, blank "8.0, 40) distance in mathematical notation. To invoke it, > print_point takes a point as an argument and displays it you can pass blank as an argument: _point(blank) (3.0, 4.0) > Inside the function, p is an alias for blank, so if the function modifies p, blank changes. 3. Rectangles > Sometimes it is obvious what the attributes of an object should be, but other times you have to make decisions. EE Ol Python - MODULE 05, For exam gine " ple, imagine you are designing a class to represent rectangles, What attributes would YOU USe Lo specify the location and size of a rectangle? You can ignore angle; to keep things simple, assume that the rectangle is either vertical or horizontal, + There are at least two possibilities: 1. You could specify one commer of the rectangle (or the center), the width, and the height 2. You could specify two opposing corners. > At this point itis hard to say whether either is better than the other, so we'll imple one, just as an example. > Here is the class definition: ment the first class Rectangle: a ‘| | ‘wRepresents a rectangle | _ attributes: width, height, comer." _| > The docstring lists the attributes: width and height are numbers; corner is a Point object that specifies the lower-left comer. + To represent rectangle, you have to instantiate a Rectangle object and assign values to the attributes: | box = Rectangle() | box. width = 100.0 | box.height = 200.0 | box.corner = Point() | box.corner.x = 0.0 box.comer.y = 0.0 > The expression box.comer.x means, “Go to the object box refers to and select the attribute named comer; then go to that object and select the attribute named x.” } Figure 15.2 shows the state of this object. An object that is an attribute of another object is embedded. Figure 15.2: Object diagram 4. Instances as return values > Functions can return instances. For example, find_center takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle: def find_center(rect): p= Point() x= rect.comer.x + rect.width/2 p.y = rect.comer.y + rect height/2 retum p Hython Mbt) Ae Wheto te ain eqauqete unt fee Law at abet ave mbit He Ce MUTIY HUH Ke py ‘eaner © tind content land tat, ptateeatgey (80, 100) § Qbiouts te mutate Vou can ehatge the state ot an ject Uy qin wn vation to ne oF He AbUtee For apt, 0 hae the size ut a rectangle wil chianlay M6 pullin, YO KH Helly the Values oF wails an heigl fo eld = fag wet 800 We ehh = fag Meth 00 ‘Yoou eat alec write fnetions that nally objects Vor exainpte, yrow rectangle takes a Mectanle object and wo autores, dvdth aud dlveiutit, and adds the nurbers to te width and eight wl the rectangle lot urow. rectanle(teed, dwidth, dlalylty ect width + dit eo Welyht + dbeighn 2 ete isan exninpe that demnstrates the effet boxsvidth, box height (150.0, 400.0) ‘drow rectangle(biox, 40, 100) ‘bon wlth, box height (200.0, 4000) | % Inside the function, root iv an allas for box, su when the funtion modifies rect, burs ehianges: 6, Copving % Allasing ean make a program difficult 10 read because changes in one place might have unexpected effects in another place. > Wishard to keep tack oF all the variables that might refer to a given object © Copying an object (soften an alternative to aliasing, The copy module conta ‘copy tht can duplicate any object function cated pl = Point() phar ko plyed0 import wwpy p2= copy copy(pl) pl and p2 contain the same data, but they are not the sane Pot snes Ub welt jail) (4 wwii Wlilyidd Hod Wl dy Falew whe Vala % 11 Ve Hyovatet tneientne att yd wna al Hm anna; id om wepoated 01H Tin baqiced—~f yb Hee tne Mowe ye seta the aa die TH Hua ess gv ot Me aldenyqetitond ty Henne Ha Har tnietanose, Mie Metall dado Hb Hie {ayn et V im wane ein Fa apse H blew speed att Me Eee Beebe Abate bnkeaiien fat ydsjatsnven ade tino Aypive, Hythe Aeieeeh 4 hueile RUA hvuthd Wik exneided eulvabent Ab eae, Ht yor Wn | wiet 40 1000 <= Wb | Wut7 fight WOU fy af AIF | I i eon Hagin 494 Cihqeet Mato HY yun tes eeapy evgey Fe ait Medan, td anid Hind ie Ht berlnn toe Mosdenigle “Aipeed nt non Abe wrshabtead Meade bein = exypy ope bide) | Wied Ve bnit Fale {faved vane ior sere | ‘we | > Figure 19-4 ehene vind the eijeed Alarm Veh tke (Hie evetabion Ua tle ahr. aden asestian Ht eoyulen Mie eine we any etonenioee H état, idl ned thes wininehdd Arvo 6 Voie mnered npplioadicitn, Hye ba Hed gee yea Ht Jn thie wenane, invededine wrvene teadongl en cain wt the Masetaniylive ain Hed wt foed thee one, as snvedenig evs pwetannl tn wnbies nel wht Hilt Thre Hstnvl Vo beni whe ered wine. 1» Hontuanabey, ia oxy made poorvidice anntine vevnacldeioerey Moe bovis Hed enby tie jae Sw whi thin eps vstnns Hy, wl the edrjaods Hy vag ti, aval ae eh Coin iH Boe igi owed to News Hind tie ogied ble de eed aad Aa Wind > nepny dasperyrylbre) bore bebo Fellow Ioan) ain one onion | ~ » aoe ond boon we eenruyladidy sagan nia topade Lime > As unother example ofa progranmer-defined type, we'll define a class called Time that records the ume of day. The class definition books like this: class Tame: “Represents the time of day atirbutes; hour, minute, second" * We can ereate a new Time obj ‘ume hour = [1 | time minute = 59 L limesecond = 30 | > The state diagram for the Time object looks like Figure below. Time time —~| hour—e 41} minule —» 59 second —» 30 2 Pure functions In the next few sections, we'll write two functions that add time values. ‘They demonstrate two kinds of functions: pure functions and modifiers. They also demonstrate a development plan Ill call prototype and patch, which is a way of tackling a complex problem by starting with a simple prototype and incrementally dealing with the complications Here is a simple prototype of ndd_time: vovvy def add_time(il, @) sum = TimeQ) sum hour = 11,hour + Q.hour sum minute © (minute + Q.minute sum.second © (1.second + 12.sccond return sum > The function creates a new Time object, initializes ils attributes, and retums a reference to the new object. This is called a pure function because it does not modify any of the objects passed to it as arguments and i has no effect, like displaying a value oF getting user input, other than returning. a value. Python MODULE 4 > To test this function, let us ereate two ‘Time objects start contains Python and the Holy Grail, and duration contains the run ime of the start time of a movie, like Monty the movie, which is one hour 35 add time figures out when the movie will be done, >> start = Time() >> starthour = 9 >> startminute » AS startsecond = 0 o> duration = Timet) >»e duration hour = | >>> duration.minute = 35 >>» duration second = 0) >>> done = add_sime(start, duration) >>> print_time(done) 10;80.00 00 might not be what you were hoping for, that this function does not deal with cases where the number of seconds or > When that happens, we have to arry" the extra seconds into the minute column or the extra minutes into the hour column, > Here's an improved version: def add_time(tl, (2): sum = Time() ‘sum.hour = t]hour + (2.hour ‘sum.minute = 1 minute + (2.minute ‘sum second = tI.second + 12.second if'sum,second >= 60: sum.second - 60 sum.minute if sum.minute >= 60: sum.minute -= 60 sum,hour += 1 return sum Python - MODULE 4 ee a &. Modifiers > Sometimes it is useful fora function to modify the objects it ges as parameters % Inthat case, the changes ar visible othe caller. Functions that work this way are called modifiers 7 inerement, Which adds a given number of seconds to a Time object, can be written naturally as a modifier. Here is a rough draft eee ane aN E ED EEINETETENE YEE SES TEES STETTEITAE def incremen(time, seconds): time second += seconds if ume.second >= 60: ume second -= 60 ftime.minute += ] if time minute >= 60° time: minute ~ 60 timehour += | Ra ‘The first line performs the basic operation; the remainder deals with the special cases we saw before 's this function correet? What happens if seconds is much greater than sixty? tn that case, it is not enough to carry once; we have to keep doin it until time second is less than sixty. One solution is to replace the if statements with while statements. That would make the function correct, but not very efficient. ‘Anything that can be done with modifiers can also be done with pure functions, 4, Prototyping versus planning + The development plan, ie. demonstrating is called “prototype and patch”. For each function, we wrote 2 prototype that performed the basic calculation and then tested it, patching errors along the way, + This approach can be effective, especially if you don't yet have a deep understanding of the problem. ~ But incremental corrections can generate code that is unnecessarily complicated—since it deals with many special cases—and unreliable—since it is hard to know if you have found all the errors, 7 Here is a function that converts Times to integers: v v v v ef time_to_int(time): sinutes = ime. hour * 69 Hime minute seconds = minutes * 60+ time second retum seconds | hon - MODULE 4 % And bere i a function that converts an integer to a Tame (recall that divmod divides the frst ‘srpiment by the second and returns the quotient and remainder as a tuple). defias to tet seconds) tame ~ Time() | ‘minutes, nme second = dinmod{seconds, 60) | tome bour, te minute = divmod{mamutes, 60) | return time + Once we are convinced they are correct, you can use them to remit: | def add_tame(t. 2): seconds = tme_to_sni(tl) + ome to m2) return int_to_time(seconds) > This version is shorter than the original, and easter to verify. © Qe Sa obpectonened programming Ligue ‘toch means at provides fume that support tosonente progammng wich ts ew tenet ~ Programs melude clas and method defniqoes 7 Metof ie cuanto serene in tem of spent cacjats SPITE ste eet a meat epi te ming nde neal world meeract > FS fencnon ats asociae witha pacula cass > Methods ar semnnaly Seca Sut there are two syntactic differences: . tedster mak fe toy erent cana te method explicit. . “Theme Gr vcking a mei iret fom he sn alg a cae Printing Objects "We ken define clas named and als wrt fantom named prt tine | (= L "Represents the time of day *** f | Pome tnetiney ; Print(%24-% 2d-% 24" °% (Cime-hour, time minute, time second)) * Tocall this function, we kave to pass a Time object as an argument | >>> start = Timegy | >>> starthour =9 >>> starlminute = 45 >>> start second = 00 >>> print_time( start) 09:45:00 Python - MODULE 4 7 Tomake pnnt_time a method, all we have to do is move the function definition inside the class definition Notice the change in indentation. class Time def print_time(time): rint('% 24:%.24:% > Now there are two ways to call print_time. The first (and less common) way is to use function syntax: >>>Time print_time(start) > Im this use of dot notation, Time is the name of the class, and print time is the name of the ‘method. start is passed as a parameter. > The second (and more concise) way is to use method syntax: > In this use of dot notation, print_time is the name of the method (again), and start is the object the method is invoked on, which is called the subject. > Just as the subject of a sentence is what the sentence is about, the subject of a method invocation is what the method is about. > Inside the method, the subject is assigned to the first parameter, so in this case start is assigned to time. By convention, the first parameter of a method is called self, soit would be more common to write print_time like this: | class Time: | def print_time(selt): print(% 2d:% 24%. 24 _% (self hour, self:minute, self second)) > The reason for this convention is an implicit metaphor: += The syntax for a function call, print_time(start), suggests that the function is the active agent. It says something like, “Hey print time! Here's an object for you to print.” + In object-oriented programming, the objects are the active agents. A method invocation like MODULE , ne SA prt tamet) anys “Hey start Please prast yourself.” Here's 2 vertaca of anegement sevesten ss 2 metho # monde class Tamme (65 sucremnen sf, wooed socom += se tame 19 e) —__ foto san 9 fame seconds) Here's bow you wuld sostihe increment: > sha prt sed) ASG 2 end ~ sta sncremensl $377, 461), Tapetiner, istemens) thes 2 possional arpumens bw} mere given ‘The eros message i tally confang, because thee ae only two arguments in yaremtesen. Ba toe siiyoc is aheo considered an arpument, so 23 sopether that's tarce By the way, 2 positoonal argument is 20 keyword argament. in tas function call. argument thet doesa't have » parnmeter sanse. that i, it is at 2 purest and cage sce penton, and dead i a keyword argument ee Python = MODULE 4 4. A More Complicated Example Rewntung 1s_after 1s slightly more camplicated because it takes two Time obyocts as parameters In this case it ts conventional to name the frst parameter seif and the second parameter ther # inside class Time. | defis_after(self, other) | retum selftime (o_int() > other.time_to_m#i{) > Tose this method, you have to invoke it on one object and pass the other as an argument nee | >>> end.is_after{start) | ae | w << eintt Method > The inst method (short for “initialization”) is a special method that gets invoked when an object igetaatione Its full mame 1s __init__(two underscore characters, followed by mit, and then two more underscores). ‘An init method for the Time class might look like this: [ # inside class Time: def _init_(self, hour=0, minute=0, second=0): selfhour = hour sel minute = minute | self.second = second Itis common for the parameters of _init__to have the same names 2s the attributes. ‘The statement ‘selfhour = hour stores the value of the parameter bour as an attribute of self. The parameters are optional, so if you call Time with no arguments, you gt the default values: [>>> me = Tuned) | | >>> ume print_tme() | 00:00:00 Ewe prone cow argeuiout, & ex eenden bow mo ne = Tae 2) SS RPK Bg) Ren > “Ewer ee argunens thy oben Bo a ine >> me $ Tina 45) Po Rate pet anne) dase AeA We prove tice argumas hey ovcende tev doth ake & The_str_ Method » rege ae kent, ta suppose 4 etm a stg cess an ofan Foeexample, Bere isa str mieten fe Time odjects # wsade class Teme. def_s_isetd) Fotam Yo 28% AE NE % (sl hour, self minute, sel seooet) ‘When you pra an abject, Python ines the stat | >>> ume = Tume(®, 43) | >>> peinytime) [Leeson 7. Operator Overloading > By defining other special metus, you can specify the behavior of operators on programmer tetiaad types > For example, if we define a method named _adkl fr the Time class, you can use the # petal of Lume objects, Here s what the definition might ook lke [Mef_add Geli | | sevonds-self tume_ to iniQorher time t0_ int) L__retumm int to_timey seconds) > And here is how we could use it > start = Timey9, 48) > duration = Tane(2, 38) Python - MODULE 4 > pntistart + duration) | 1.2000 | » Whom you apply the + operator to Time objects, Python invokes __add_. > When you print the result, Python invokes _ str__ So there is a lot happening behind the scenes! > Changing the behavior of an operator so that it works wath programmersefined types 18 called eperaior overkoading . For every operator mt Python there ts a corresponding special method, tke _ald_. 8. Type-Based Dispatch > The folowing 1 the version of add that checks the type of other and invokes either adld_time or sncromacat def__add_(self,other): if isintance(other, Time): reaum self.ndd_time(other) | else: | | return self increment(other) | defadd_time(self, other): [7 seconds = self tie. to_int) + othertime_to int) | retum int_to_time( seconds) def increment(self, seconds): seconds + selftime_to_int() | retumn int_to_ time seconds) } The built-in function isinstance takes a value and a class object, and returas True if the value is an instance of the class. > Ifother is a Tame object, —add__invokes add_time. Otherwise it assumes thatthe parameter is a number and invokes increment. > This operation is called a type-based dispatch because it dispatches the computation to different methods based on the type of the arguments. > Here are examples that use the + operator with different >>> start = Tame(9, 45) | >>> duration = Tume(1, 35) >>> print(start + duration) 11:20:00 >>> print(start + 1337) ORT | 2 Untomamnery (os openers of addin ts wo comemnateve. 1 the integer “> pam 337 + seat Tipetrron aneuppores operand tyyets) for + mt and ‘instance’ 7 The proton is, Te of ashing the Time cbse wo add an integer, Pybom is aoking am imieyer to add Time object, and i doesa't kom hon perand. you yer » Bas Shere i = Sever shai for ths problem the special method _sadd_, which stands for “right-side > This method imwched when 9 Time object appears om the sight side of the + operator Here's the * imide class Fone - 48 _sadd_(selt, ottery Fetarn “AK. add (othery | 7 Aad here's how it’s used: > print 1337 + stant) son747 9. Rolymorphism 7 carro a Sette tl he snc bt analy trp ery Often y San Oo by wing Kens that or cotely or aeguments with iferet es " , Macy of the Sanctions we wrete fon strings also work for other bstorgzan to cont the mmber of times each leter appears in 2 sequence types. For example, we used word, ~ — ———— ustergrarn sy. d= dict) for ¢ ins: ife not ind dfej=t else fc} = dfc}+t tetarn d 7 This function also works for lists, tuples, and even dictionanes, ns Jong as the elements of s are hashable, so they can tre used as keys in a facile code neue. -) Fanctions that work wits several types are called puiysmorpluc. Polymorgiism cart works as Song as the > Fos ceample, the fuiltés fimetion suns, who's ads the clemencs of 4 seqnesie. cements of the sequence support adios por = FimetT, 45 pop C= Tomel7, 31} pom B= Timet?, 37) o> wotad = surat, 2, >> paints} B0t00 orks with that r be pce io he epereionn isd 2 fancon work wi ven pe Kee oe The best kind of polymorphises is he unmtentional ‘wrote ean be applied wo 2 type you never planned for. unc, here you discover that afc tm yor ale

You might also like