SLIDE 1
61A Lecture 16 Announcements String Representations String - - PowerPoint PPT Presentation
61A Lecture 16 Announcements String Representations String - - PowerPoint PPT Presentation
61A Lecture 16 Announcements String Representations String Representations 4 String Representations An object value should behave like the kind of data it is meant to represent 4 String Representations An object value should behave like the
SLIDE 2
SLIDE 3
String Representations
SLIDE 4
String Representations
4
SLIDE 5
String Representations
An object value should behave like the kind of data it is meant to represent
4
SLIDE 6
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
4
SLIDE 7
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 Strings are important: they represent language and programs
4
SLIDE 8
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 Strings are important: they represent language and programs In Python, all objects produce two string representations:
4
SLIDE 9
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 Strings are important: they represent language and programs In Python, all objects produce two string representations:
- The str is legible to humans
4
SLIDE 10
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 Strings are important: they represent language and programs In Python, all objects produce two string representations:
- The str is legible to humans
- The repr is legible to the Python interpreter
4
SLIDE 11
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 Strings are important: they represent language and programs In Python, all objects produce two string representations:
- The str is legible to humans
- The repr is legible to the Python interpreter
The str and repr strings are often the same, but not always
4
SLIDE 12
The repr String for an Object
5
SLIDE 13
The repr String for an Object
The repr function returns a Python expression (a string) that evaluates to an equal object
5
SLIDE 14
The repr String for an Object
repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The repr function returns a Python expression (a string) that evaluates to an equal object
5
SLIDE 15
The repr String for an Object
The result of calling repr on a value is what Python prints in an interactive session repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The repr function returns a Python expression (a string) that evaluates to an equal object
5
SLIDE 16
The repr String for an Object
The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The repr function returns a Python expression (a string) that evaluates to an equal object
5
SLIDE 17
The repr String for an Object
The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 12000000000000.0 repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The repr function returns a Python expression (a string) that evaluates to an equal object
5
SLIDE 18
The repr String for an Object
The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 12000000000000.0 >>> print(repr(12e12)) repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The repr function returns a Python expression (a string) that evaluates to an equal object
5
SLIDE 19
The repr String for an Object
The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 12000000000000.0 >>> print(repr(12e12)) 12000000000000.0 repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The repr function returns a Python expression (a string) that evaluates to an equal object
5
SLIDE 20
The repr String for an Object
The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 12000000000000.0 >>> print(repr(12e12)) 12000000000000.0 Some objects do not have a simple Python-readable string repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The repr function returns a Python expression (a string) that evaluates to an equal object
5
SLIDE 21
The repr String for an Object
The result of calling repr on a value is what Python prints in an interactive session >>> 12e12 12000000000000.0 >>> print(repr(12e12)) 12000000000000.0 Some objects do not have a simple Python-readable string repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. The repr function returns a Python expression (a string) that evaluates to an equal object >>> repr(min) '<built-in function min>'
5
SLIDE 22
The str String for an Object
Human interpretable strings are useful as well:
6
SLIDE 23
The str String for an Object
Human interpretable strings are useful as well: >>> from fractions import Fraction
6
SLIDE 24
The str String for an Object
Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2)
6
SLIDE 25
The str String for an Object
Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half)
6
SLIDE 26
The str String for an Object
Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)'
6
SLIDE 27
The str String for an Object
Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half)
6
SLIDE 28
The str String for an Object
Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) '1/2'
6
SLIDE 29
The str String for an Object
Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) '1/2' The result of calling str on the value of an expression is what Python prints using the print function:
6
SLIDE 30
The str String for an Object
Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) '1/2' The result of calling str on the value of an expression is what Python prints using the print function:
6
>>> print(half)
SLIDE 31
The str String for an Object
Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) '1/2' The result of calling str on the value of an expression is what Python prints using the print function:
6
>>> print(half) 1/2
SLIDE 32
The str String for an Object
Human interpretable strings are useful as well: >>> from fractions import Fraction >>> half = Fraction(1, 2) >>> repr(half) 'Fraction(1, 2)' >>> str(half) '1/2' (Demo) The result of calling str on the value of an expression is what Python prints using the print function:
6
>>> print(half) 1/2
SLIDE 33
Polymorphic Functions
SLIDE 34
Polymorphic Functions
8
SLIDE 35
Polymorphic Functions
Polymorphic function: A function that applies to many (poly) different forms (morph) of data
8
SLIDE 36
Polymorphic Functions
Polymorphic function: A function that applies to many (poly) different forms (morph) of data str and repr are both polymorphic; they apply to any object
8
SLIDE 37
Polymorphic Functions
Polymorphic function: A function that applies to many (poly) different forms (morph) of data str and repr are both polymorphic; they apply to any object repr invokes a zero-argument method __repr__ on its argument
8
SLIDE 38
Polymorphic Functions
Polymorphic function: A function that applies to many (poly) different forms (morph) of data str and repr are both polymorphic; they apply to any object repr invokes a zero-argument method __repr__ on its argument >>> half.__repr__() 'Fraction(1, 2)'
8
SLIDE 39
Polymorphic Functions
Polymorphic function: A function that applies to many (poly) different forms (morph) of data str and repr are both polymorphic; they apply to any object repr invokes a zero-argument method __repr__ on its argument str invokes a zero-argument method __str__ on its argument >>> half.__repr__() 'Fraction(1, 2)'
8
SLIDE 40
Polymorphic Functions
Polymorphic function: A function that applies to many (poly) different forms (morph) of data str and repr are both polymorphic; they apply to any object repr invokes a zero-argument method __repr__ on its argument str invokes a zero-argument method __str__ on its argument >>> half.__repr__() 'Fraction(1, 2)' >>> half.__str__() '1/2'
8
SLIDE 41
Implementing repr and str
9
SLIDE 42
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
9
SLIDE 43
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
- An instance attribute called __repr__ is ignored! Only class attributes are found
9
SLIDE 44
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
- An instance attribute called __repr__ is ignored! Only class attributes are found
- Question: How would we implement this behavior?
9
SLIDE 45
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
- An instance attribute called __repr__ is ignored! Only class attributes are found
- Question: How would we implement this behavior?
9
def repr(x): return type(x).__repr__(x) def repr(x): return x.__repr__(x) def repr(x): return x.__repr__() def repr(x): return type(x).__repr__() def repr(x): return super(x).__repr__()
SLIDE 46
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
- An instance attribute called __repr__ is ignored! Only class attributes are found
- Question: How would we implement this behavior?
9
def repr(x): return type(x).__repr__(x) def repr(x): return x.__repr__(x) def repr(x): return x.__repr__() def repr(x): return type(x).__repr__() def repr(x): return super(x).__repr__()
SLIDE 47
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
- An instance attribute called __repr__ is ignored! Only class attributes are found
- Question: How would we implement this behavior?
9
def repr(x): return type(x).__repr__(x) def repr(x): return x.__repr__(x) def repr(x): return x.__repr__() def repr(x): return type(x).__repr__() def repr(x): return super(x).__repr__() The behavior of str is also complicated:
SLIDE 48
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
- An instance attribute called __repr__ is ignored! Only class attributes are found
- Question: How would we implement this behavior?
9
def repr(x): return type(x).__repr__(x) def repr(x): return x.__repr__(x) def repr(x): return x.__repr__() def repr(x): return type(x).__repr__() def repr(x): return super(x).__repr__() The behavior of str is also complicated:
- An instance attribute called __str__ is ignored
SLIDE 49
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
- An instance attribute called __repr__ is ignored! Only class attributes are found
- Question: How would we implement this behavior?
9
def repr(x): return type(x).__repr__(x) def repr(x): return x.__repr__(x) def repr(x): return x.__repr__() def repr(x): return type(x).__repr__() def repr(x): return super(x).__repr__() The behavior of str is also complicated:
- An instance attribute called __str__ is ignored
- If no __str__ attribute is found, uses repr string
SLIDE 50
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
- An instance attribute called __repr__ is ignored! Only class attributes are found
- Question: How would we implement this behavior?
9
def repr(x): return type(x).__repr__(x) def repr(x): return x.__repr__(x) def repr(x): return x.__repr__() def repr(x): return type(x).__repr__() def repr(x): return super(x).__repr__() The behavior of str is also complicated:
- An instance attribute called __str__ is ignored
- If no __str__ attribute is found, uses repr string
- (By the way, str is a class, not a function)
SLIDE 51
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
- An instance attribute called __repr__ is ignored! Only class attributes are found
- Question: How would we implement this behavior?
9
def repr(x): return type(x).__repr__(x) def repr(x): return x.__repr__(x) def repr(x): return x.__repr__() def repr(x): return type(x).__repr__() def repr(x): return super(x).__repr__() The behavior of str is also complicated:
- An instance attribute called __str__ is ignored
- If no __str__ attribute is found, uses repr string
- (By the way, str is a class, not a function)
- Question: How would we implement this behavior?
SLIDE 52
Implementing repr and str
The behavior of repr is slightly more complicated than invoking __repr__ on its argument:
- An instance attribute called __repr__ is ignored! Only class attributes are found
- Question: How would we implement this behavior?
9
(Demo) def repr(x): return type(x).__repr__(x) def repr(x): return x.__repr__(x) def repr(x): return x.__repr__() def repr(x): return type(x).__repr__() def repr(x): return super(x).__repr__() The behavior of str is also complicated:
- An instance attribute called __str__ is ignored
- If no __str__ attribute is found, uses repr string
- (By the way, str is a class, not a function)
- Question: How would we implement this behavior?
SLIDE 53
Interfaces
10
SLIDE 54
Interfaces
Message passing: Objects interact by looking up attributes on each other (passing messages)
10
SLIDE 55
Interfaces
Message passing: Objects interact by looking up attributes on each other (passing messages) The attribute look-up rules allow different data types to respond to the same message
10
SLIDE 56
Interfaces
Message passing: Objects interact by looking up attributes on each other (passing messages) The attribute look-up rules allow different data types to respond to the same message A shared message (attribute name) that elicits similar behavior from different object classes is a powerful method of abstraction
10
SLIDE 57
Interfaces
Message passing: Objects interact by looking up attributes on each other (passing messages) The attribute look-up rules allow different data types to respond to the same message A shared message (attribute name) 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
10
SLIDE 58
Interfaces
Message passing: Objects interact by looking up attributes on each other (passing messages) The attribute look-up rules allow different data types to respond to the same message A shared message (attribute name) 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 Example:
10
SLIDE 59
Interfaces
Message passing: Objects interact by looking up attributes on each other (passing messages) The attribute look-up rules allow different data types to respond to the same message A shared message (attribute name) 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 Example: Classes that implement __repr__ and __str__ methods that return Python-interpretable and human-readable strings implement an interface for producing string representations
10
SLIDE 60
Interfaces
Message passing: Objects interact by looking up attributes on each other (passing messages) The attribute look-up rules allow different data types to respond to the same message A shared message (attribute name) 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 Example: Classes that implement __repr__ and __str__ methods that return Python-interpretable and human-readable strings implement an interface for producing string representations
10
(Demo)
SLIDE 61
Special Method Names
SLIDE 62
Special Method Names in Python
12
SLIDE 63
Special Method Names in Python
12
Certain names are special because they have built-in behavior
SLIDE 64
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores
SLIDE 65
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__
SLIDE 66
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ Method invoked automatically when an object is constructed
SLIDE 67
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ Method invoked automatically when an object is constructed
SLIDE 68
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression
SLIDE 69
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression
SLIDE 70
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another
SLIDE 71
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another
SLIDE 72
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another Method invoked to convert an object to True or False
SLIDE 73
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ __float__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another Method invoked to convert an object to True or False
SLIDE 74
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ __float__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another Method invoked to convert an object to True or False Method invoked to convert an object to a float (real number)
SLIDE 75
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ __float__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another Method invoked to convert an object to True or False Method invoked to convert an object to a float (real number) >>> zero, one, two = 0, 1, 2
SLIDE 76
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ __float__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another Method invoked to convert an object to True or False Method invoked to convert an object to a float (real number) >>> zero, one, two = 0, 1, 2 >>> one + two 3
SLIDE 77
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ __float__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another Method invoked to convert an object to True or False Method invoked to convert an object to a float (real number) >>> zero, one, two = 0, 1, 2 >>> one + two 3 >>> bool(zero), bool(one) (False, True)
SLIDE 78
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ __float__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another Method invoked to convert an object to True or False Method invoked to convert an object to a float (real number) >>> zero, one, two = 0, 1, 2 >>> one + two 3 >>> bool(zero), bool(one) (False, True) Same behavior using methods
SLIDE 79
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ __float__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another Method invoked to convert an object to True or False Method invoked to convert an object to a float (real number) >>> zero, one, two = 0, 1, 2 >>> one + two 3 >>> bool(zero), bool(one) (False, True) >>> zero, one, two = 0, 1, 2 Same behavior using methods
SLIDE 80
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ __float__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another Method invoked to convert an object to True or False Method invoked to convert an object to a float (real number) >>> zero, one, two = 0, 1, 2 >>> one + two 3 >>> bool(zero), bool(one) (False, True) >>> zero, one, two = 0, 1, 2 >>> one.__add__(two) 3 Same behavior using methods
SLIDE 81
Special Method Names in Python
12
Certain names are special because they have built-in behavior These names always start and end with two underscores __init__ __repr__ __add__ __bool__ __float__ Method invoked automatically when an object is constructed Method invoked to display an object as a Python expression Method invoked to add one object to another Method invoked to convert an object to True or False Method invoked to convert an object to a float (real number) >>> zero, one, two = 0, 1, 2 >>> one + two 3 >>> bool(zero), bool(one) (False, True) >>> zero, one, two = 0, 1, 2 >>> one.__add__(two) 3 >>> zero.__bool__(), one.__bool__() (False, True) Same behavior using methods
SLIDE 82
Special Methods
13
SLIDE 83
Special Methods
Adding instances of user-defined classes invokes either the __add__ or __radd__ method
13
SLIDE 84
Special Methods
Adding instances of user-defined classes invokes either the __add__ or __radd__ method >>> Ratio(1, 3) + Ratio(1, 6) Ratio(1, 2)
13
SLIDE 85
Special Methods
Adding instances of user-defined classes invokes either the __add__ or __radd__ method >>> Ratio(1, 3) + Ratio(1, 6) Ratio(1, 2)
13
>>> Ratio(1, 3).__add__(Ratio(1, 6)) Ratio(1, 2)
SLIDE 86
Special Methods
Adding instances of user-defined classes invokes either the __add__ or __radd__ method >>> Ratio(1, 3) + Ratio(1, 6) Ratio(1, 2)
13
>>> Ratio(1, 3).__add__(Ratio(1, 6)) Ratio(1, 2) >>> Ratio(1, 6).__radd__(Ratio(1, 3)) Ratio(1, 2)
SLIDE 87
Special Methods
Adding instances of user-defined classes invokes either the __add__ or __radd__ method >>> Ratio(1, 3) + Ratio(1, 6) Ratio(1, 2)
13
http://docs.python.org/py3k/reference/datamodel.html#special-method-names http://getpython3.com/diveintopython3/special-method-names.html
>>> Ratio(1, 3).__add__(Ratio(1, 6)) Ratio(1, 2) >>> Ratio(1, 6).__radd__(Ratio(1, 3)) Ratio(1, 2)
SLIDE 88
Special Methods
Adding instances of user-defined classes invokes either the __add__ or __radd__ method >>> Ratio(1, 3) + Ratio(1, 6) Ratio(1, 2)
13
http://docs.python.org/py3k/reference/datamodel.html#special-method-names http://getpython3.com/diveintopython3/special-method-names.html
>>> Ratio(1, 3).__add__(Ratio(1, 6)) Ratio(1, 2) >>> Ratio(1, 6).__radd__(Ratio(1, 3)) Ratio(1, 2) (Demo)
SLIDE 89
Generic Functions
14
SLIDE 90
Generic Functions
A polymorphic function might take two or more arguments of different types
14
SLIDE 91
Generic Functions
A polymorphic function might take two or more arguments of different types Type Dispatching: Inspect the type of an argument in order to select behavior
14
SLIDE 92
Generic Functions
A polymorphic function might take two or more arguments of different types Type Dispatching: Inspect the type of an argument in order to select behavior Type Coercion: Convert one value to match the type of another
14
SLIDE 93
Generic Functions
A polymorphic function might take two or more arguments of different types Type Dispatching: Inspect the type of an argument in order to select behavior Type Coercion: Convert one value to match the type of another
14
>>> Ratio(1, 3) + 1 Ratio(4, 3)
SLIDE 94
Generic Functions
A polymorphic function might take two or more arguments of different types Type Dispatching: Inspect the type of an argument in order to select behavior Type Coercion: Convert one value to match the type of another
14
>>> Ratio(1, 3) + 1 Ratio(4, 3) >>> 1 + Ratio(1, 3) Ratio(4, 3)
SLIDE 95
Generic Functions
A polymorphic function might take two or more arguments of different types Type Dispatching: Inspect the type of an argument in order to select behavior Type Coercion: Convert one value to match the type of another
14
>>> Ratio(1, 3) + 1 Ratio(4, 3) >>> 1 + Ratio(1, 3) Ratio(4, 3) >>> from math import pi >>> Ratio(1, 3) + pi 3.4749259869231266
SLIDE 96
Generic Functions
A polymorphic function might take two or more arguments of different types Type Dispatching: Inspect the type of an argument in order to select behavior Type Coercion: Convert one value to match the type of another
14