61a lecture 14
play

61A LECTURE 14 An abstrac/on might have more than one - PDF document

7/16/13 Generic Functions 61A LECTURE 14 An abstrac/on might have more than one representa/on. MULTIPLE Python has many sequence types: tuples, ranges, lists, etc.


  1. 7/16/13 ¡ Generic Functions 61A LECTURE 14 – An ¡abstrac/on ¡might ¡have ¡more ¡than ¡one ¡representa/on. ¡ MULTIPLE • Python ¡has ¡many ¡sequence ¡types: ¡tuples, ¡ranges, ¡lists, ¡etc. ¡ ¡ REPRESENTATIONS An ¡abstract ¡data ¡type ¡might ¡have ¡mul/ple ¡implementa/ons. ¡ • Some ¡representa/ons ¡are ¡beBer ¡suited ¡to ¡some ¡problems ¡ Steven Tang and Eric Tzeng ¡ July 17, 2013 A ¡func/on ¡might ¡want ¡to ¡operate ¡on ¡mul/ple ¡data ¡types. ¡ ¡ Message ¡passing ¡enables ¡us ¡to ¡accomplish ¡all ¡of ¡the ¡above, ¡as ¡ we ¡will ¡see ¡today ¡and ¡next ¡/me ¡ String Representations Message Passing Enables Polymorphism An ¡object ¡value ¡should ¡ behave ¡like ¡the ¡kind ¡of ¡data ¡it ¡is ¡meant ¡ Polymorphic ¡func/on: ¡A ¡func/on ¡that ¡can ¡be ¡applied ¡to ¡many ¡ to ¡represent; ¡ ( poly ) ¡different ¡forms ¡( morph ) ¡of ¡data ¡ For ¡instance, ¡by ¡ producing ¡a ¡string ¡ representa/on ¡of ¡itself. ¡ str ¡and ¡ repr ¡are ¡both ¡polymorphic; ¡they ¡apply ¡to ¡anything. ¡ Strings ¡are ¡important: ¡they ¡represent ¡ language ¡and ¡ programs . ¡ repr ¡invokes ¡a ¡zero-­‑argument ¡method ¡ __repr__ ¡on ¡its ¡ In ¡Python, ¡all ¡objects ¡produce ¡two ¡string ¡representa/ons: ¡ argument. ¡ • The ¡“str” ¡is ¡legible ¡to ¡ humans . ¡ >>> ¡today.__repr__() ¡ • The ¡“repr” ¡is ¡legible ¡to ¡the ¡ Python ¡interpreter . ¡ 'datetime.date(2013, ¡7, ¡16)' ¡ “str” ¡and ¡“repr” ¡strings ¡are ¡oNen ¡the ¡same! ¡Think: ¡numbers. ¡ str ¡invokes ¡a ¡zero-­‑argument ¡method ¡ __str__ ¡on ¡its ¡ argument. ¡(But ¡ str ¡is ¡a ¡class, ¡not ¡a ¡func/on!) ¡ When ¡the ¡“str” ¡and ¡“repr” ¡ strings ¡are ¡the ¡same , ¡that’s ¡evidence ¡ >>> ¡today.__str__() ¡ that ¡a ¡programming ¡language ¡is ¡legible ¡by ¡humans! ¡ '2013-­‑07-­‑16' ¡ Inheritance and Polymorphism Aside: duck typing Inheritance also enables polymorphism, since subclasses provide at least as much behavior as their base classes • “If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.” Example of function that works on all accounts: • In Python terms: it doesn’t matter what the official type of def welfare(account): something is if it understands the correct messages """Deposit $100 into an account if it has less • The repr function knows nothing about the types of than $100.""" objects that it’s getting… if account.balance < 100: return account.deposit(100) • …except that they can quack (they have a __repr__ method). >>> ¡alice_account ¡= ¡CheckingAccount(0) ¡ >>> ¡welfare(alice_account) ¡ 100 ¡ >>> ¡bob_account ¡= ¡SavingsAccount(0) ¡ >>> ¡welfare(bob_account) ¡ 98 ¡ 1 ¡

  2. 7/16/13 ¡ Interfaces Special Methods Message ¡passing ¡allows ¡ different ¡data ¡types ¡to ¡respond ¡to ¡the ¡ same ¡message . ¡ Python operators and generic functions make use of methods with names like “ __name__ ” A ¡shared ¡message ¡that ¡elicits ¡similar ¡behavior ¡from ¡different ¡ These are special or magic methods object ¡classes ¡is ¡a ¡powerful ¡method ¡of ¡abstrac/on. ¡ Examples: An ¡ interface ¡is ¡a ¡ set ¡of ¡shared ¡messages , ¡along ¡with ¡a ¡ specifica/on ¡of ¡ what ¡they ¡mean . ¡ len __len__ +, += __add__, __iadd__ Classes ¡that ¡implement ¡ __repr__ ¡and ¡ __str__ ¡methods ¡ that ¡return ¡Python-­‑ ¡and ¡human-­‑readable ¡strings ¡thereby ¡ [], []= __getitem__, __setitem__ implement ¡an ¡interface ¡for ¡producing ¡Python ¡string ¡ . __getattr__, representa/ons. ¡ __setattr__ Classes ¡that ¡implement ¡ __len__ ¡and ¡ __getitem__ ¡are ¡ sequences. ¡ ¡ Example: Rational Numbers Property Methods ONen, ¡we ¡want ¡the ¡value ¡of ¡instance ¡aBributes ¡to ¡be ¡linked. ¡ class Rational(object): def __init__(self, numer, denom): g = gcd(numer, denom) >>> ¡f ¡= ¡Rational(3, ¡5) ¡ self.numerator = numer // g >>> ¡f.float_value ¡ self.denominator = denom // g 0.6 ¡ @property def __repr__(self): >>> ¡f.numerator ¡= ¡4 ¡ return 'Rational({0}, {1})'.format(self.numerator, def float_value(self): >>> ¡f.float_value ¡ self.denominator) return (self.numerator // 0.8 ¡ def __str__(self): self.denominator) >>> ¡f.denominator ¡-­‑= ¡3 ¡ return '{0}/{1}'.format(self.numerator, >>> ¡f.float_value ¡ self.denominator) def __add__(self, num): 2.0 ¡ denom = self.denominator * num.denominator The ¡ @property ¡decorator ¡on ¡a ¡method ¡designates ¡that ¡it ¡will ¡ numer1 = self.numerator * num.denominator numer2 = self.denominator * num.numerator be ¡called ¡whenever ¡it ¡is ¡ looked ¡up ¡on ¡an ¡instance. ¡ ¡ return Rational(numer1 + numer2, denom) def __eq__(self, num): It ¡allows ¡zero-­‑argument ¡methods ¡to ¡be ¡called ¡without ¡an ¡explicit ¡ return (self.numerator == num.numerator and call ¡expression. ¡ self.denominator == num.denominator) Multiple Representations of Abstract Data Arithmetic Abstraction Barriers Rectangular ¡and ¡polar ¡representa/ons ¡for ¡complex ¡numbers ¡ Complex ¡numbers ¡as ¡whole ¡data ¡values ¡ add_complex ¡ ¡mul_complex ¡ Complex ¡numbers ¡as ¡two-­‑dimensional ¡vectors ¡ real ¡ ¡imag ¡ ¡magnitude ¡ ¡angle ¡ Rectangular ¡ Polar ¡ representa=on ¡ representa=on ¡ Most ¡opera/ons ¡don't ¡care ¡about ¡the ¡representa/on. ¡ Some ¡mathema/cal ¡opera/ons ¡are ¡easier ¡on ¡one ¡than ¡the ¡other. ¡ 2 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend