61A Lecture 16 - inst.eecs.berkeley.edu

96
61A Lecture 16

Transcript of 61A Lecture 16 - inst.eecs.berkeley.edu

Page 1: 61A Lecture 16 - inst.eecs.berkeley.edu

61A Lecture 16

Page 2: 61A Lecture 16 - inst.eecs.berkeley.edu

Announcements

Page 3: 61A Lecture 16 - inst.eecs.berkeley.edu

String Representations

Page 4: 61A Lecture 16 - inst.eecs.berkeley.edu

String Representations

4

Page 5: 61A Lecture 16 - inst.eecs.berkeley.edu

String Representations

An object value should behave like the kind of data it is meant to represent

4

Page 6: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 7: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 8: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 9: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 10: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 11: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 12: 61A Lecture 16 - inst.eecs.berkeley.edu

The repr String for an Object

5

Page 13: 61A Lecture 16 - inst.eecs.berkeley.edu

The repr String for an Object

The repr function returns a Python expression (a string) that evaluates to an equal object

5

Page 14: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 15: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 16: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 17: 61A Lecture 16 - inst.eecs.berkeley.edu

The repr String for an Object

The result of calling repr on a value is what Python prints in an interactive session

>>> 12e1212000000000000.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

Page 18: 61A Lecture 16 - inst.eecs.berkeley.edu

The repr String for an Object

The result of calling repr on a value is what Python prints in an interactive session

>>> 12e1212000000000000.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

Page 19: 61A Lecture 16 - inst.eecs.berkeley.edu

The repr String for an Object

The result of calling repr on a value is what Python prints in an interactive session

>>> 12e1212000000000000.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

Page 20: 61A Lecture 16 - inst.eecs.berkeley.edu

The repr String for an Object

The result of calling repr on a value is what Python prints in an interactive session

>>> 12e1212000000000000.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

Page 21: 61A Lecture 16 - inst.eecs.berkeley.edu

The repr String for an Object

The result of calling repr on a value is what Python prints in an interactive session

>>> 12e1212000000000000.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

Page 22: 61A Lecture 16 - inst.eecs.berkeley.edu

The str String for an Object

Human interpretable strings are useful as well:

6

Page 23: 61A Lecture 16 - inst.eecs.berkeley.edu

The str String for an Object

Human interpretable strings are useful as well:

>>> from fractions import Fraction

6

Page 24: 61A Lecture 16 - inst.eecs.berkeley.edu

The str String for an Object

Human interpretable strings are useful as well:

>>> from fractions import Fraction>>> half = Fraction(1, 2)

6

Page 25: 61A Lecture 16 - inst.eecs.berkeley.edu

The str String for an Object

Human interpretable strings are useful as well:

>>> from fractions import Fraction>>> half = Fraction(1, 2)>>> repr(half)

6

Page 26: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 27: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 28: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 29: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 30: 61A Lecture 16 - inst.eecs.berkeley.edu

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)

Page 31: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 32: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 33: 61A Lecture 16 - inst.eecs.berkeley.edu

Polymorphic Functions

Page 34: 61A Lecture 16 - inst.eecs.berkeley.edu

Polymorphic Functions

8

Page 35: 61A Lecture 16 - inst.eecs.berkeley.edu

Polymorphic Functions

Polymorphic function: A function that applies to many (poly) different forms (morph) of data

8

Page 36: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 37: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 38: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 39: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 40: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 41: 61A Lecture 16 - inst.eecs.berkeley.edu

Implementing repr and str

9

Page 42: 61A Lecture 16 - inst.eecs.berkeley.edu

Implementing repr and str

The behavior of repr is slightly more complicated than invoking __repr__ on its argument:

9

Page 43: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 44: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 45: 61A Lecture 16 - inst.eecs.berkeley.edu

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__()

Page 46: 61A Lecture 16 - inst.eecs.berkeley.edu

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__()

Page 47: 61A Lecture 16 - inst.eecs.berkeley.edu

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:

Page 48: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 49: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 50: 61A Lecture 16 - inst.eecs.berkeley.edu

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)

Page 51: 61A Lecture 16 - inst.eecs.berkeley.edu

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?

Page 52: 61A Lecture 16 - inst.eecs.berkeley.edu

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?

Page 53: 61A Lecture 16 - inst.eecs.berkeley.edu

Interfaces

10

Page 54: 61A Lecture 16 - inst.eecs.berkeley.edu

Interfaces

Message passing: Objects interact by looking up attributes on each other (passing messages)

10

Page 55: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 56: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 57: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 58: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 59: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 60: 61A Lecture 16 - inst.eecs.berkeley.edu

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)

Page 61: 61A Lecture 16 - inst.eecs.berkeley.edu

Special Method Names

Page 62: 61A Lecture 16 - inst.eecs.berkeley.edu

Special Method Names in Python

12

Page 63: 61A Lecture 16 - inst.eecs.berkeley.edu

Special Method Names in Python

12

Certain names are special because they have built-in behavior

Page 64: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 65: 61A Lecture 16 - inst.eecs.berkeley.edu

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__

Page 66: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 67: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 68: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 69: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 70: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 71: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 72: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 73: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 74: 61A Lecture 16 - inst.eecs.berkeley.edu

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)

Page 75: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 76: 61A Lecture 16 - inst.eecs.berkeley.edu

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 + two3

Page 77: 61A Lecture 16 - inst.eecs.berkeley.edu

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 + two3>>> bool(zero), bool(one)(False, True)

Page 78: 61A Lecture 16 - inst.eecs.berkeley.edu

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 + two3>>> bool(zero), bool(one)(False, True)

Same behavior using

methods

Page 79: 61A Lecture 16 - inst.eecs.berkeley.edu

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 + two3>>> bool(zero), bool(one)(False, True)

>>> zero, one, two = 0, 1, 2Same

behavior using

methods

Page 80: 61A Lecture 16 - inst.eecs.berkeley.edu

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 + two3>>> bool(zero), bool(one)(False, True)

>>> zero, one, two = 0, 1, 2>>> one.__add__(two)3

Same behavior using

methods

Page 81: 61A Lecture 16 - inst.eecs.berkeley.edu

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 + two3>>> 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

Page 82: 61A Lecture 16 - inst.eecs.berkeley.edu

Special Methods

13

Page 83: 61A Lecture 16 - inst.eecs.berkeley.edu

Special Methods

Adding instances of user-defined classes invokes either the __add__ or __radd__ method

13

Page 84: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 85: 61A Lecture 16 - inst.eecs.berkeley.edu

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)

Page 86: 61A Lecture 16 - inst.eecs.berkeley.edu

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)

Page 87: 61A Lecture 16 - inst.eecs.berkeley.edu

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)

Page 88: 61A Lecture 16 - inst.eecs.berkeley.edu

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)

Page 89: 61A Lecture 16 - inst.eecs.berkeley.edu

Generic Functions

14

Page 90: 61A Lecture 16 - inst.eecs.berkeley.edu

Generic Functions

A polymorphic function might take two or more arguments of different types

14

Page 91: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 92: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 93: 61A Lecture 16 - inst.eecs.berkeley.edu

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)

Page 94: 61A Lecture 16 - inst.eecs.berkeley.edu

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)

Page 95: 61A Lecture 16 - inst.eecs.berkeley.edu

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

Page 96: 61A Lecture 16 - inst.eecs.berkeley.edu

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

(Demo)

>>> Ratio(1, 3) + 1 Ratio(4, 3)

>>> 1 + Ratio(1, 3) Ratio(4, 3)

>>> from math import pi >>> Ratio(1, 3) + pi 3.4749259869231266