CS61A Lecture 20: Multiple Representations

5 Pages
51 Views
Unlock Document

Department
Computer Science
Course
COMPSCI 61A
Professor
De Nero
Semester
Fall

Description
Generic Functions An abstraction might have more than one representation. Python has many sequence types: tuples, ranges, lists, etc. An abstract data type might have multiple implementations Some representations are better suited to some problems. A function might want to operate on multiple data types. String Representations An object value should behave like the kind of data it is meant to represent; For instance, by producing a string representation of itself String are important: represent language and programs In Python, all objects produce two string representations: "str" legible to humans "repr" legible to the Python Interpreter When "str" and "repr" string are the same, that's a sign that a programming language is legible to humans The "repr" String for an Object The repr function returns a Python expression (as a string) that evaluates to an equal object repr(object) --> string Return canonical string representation of object for most object types. eval(repr(object)) == object The result of calling repr on the value of expression is what Python prints in an interactive session 12e12 120000000000.0 print(repr(12e12)) 120000000000.0 Some objects don't have a simple Python-readable string repr(min) '' Human interpretable strings are useful as well import datetime today = datetime.date(2013, 10, 21) repr(today) 'datetime.date(2013, 10, 21)' str(today) '2013-10-21' Result of calling str on value of expression is what Python prints using print function Use '{0} is today'.format(today) Polymorphic Functions A function that can be applied to many (poly) different forms (morph) of data str and repr are both polymorphic; they apply to anything repr invokes a zero-argument method __repr__ on its argument str invokes a zero-argument method __str__ on its argument Implementing repr and str The behavior of repr slightly more complicated than invoking __repr__ An instance attribute called repr ignored Question: How would we implement behavior? The behavior of str: An instance attribute called __str__ is ignored If no __str__ attribute is found, uses repr string. str is class not function def repr(x): return type(x).__repr__(x) def str(x): t = type(x) if hasattr(t, '__str__'): return t.__str__(x) else: return user_repr(x) class Bear: "A bear." oski = Bear() Bear.__repr__ = lambda self: 'Bear()' oski.__repr__ = lambda: 'oski' Bear.__str__ = lambda self: 'Grr' oski.__str__ = lambda: 'Go Bears' Interfaces Message passing: Objects interact by calling named methods on each other Message passing allows different data types to respond to the same message. A shared message that elicits similar behavior from different object classes is a powerful method of abstraction An interface is a set of shared messages, along with a specification of what they mean. Examples: Classes that implement __repr__ and __str__ methods that return Python
More Less

Related notes for COMPSCI 61A

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit