61A Lecture 16 Announcements String Representations String - - PowerPoint PPT Presentation

61a lecture 16 announcements string representations
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

61A Lecture 16

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

String Representations

slide-4
SLIDE 4

String Representations

4

slide-5
SLIDE 5

String Representations

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

4

slide-6
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
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
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
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
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
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
SLIDE 12

The repr String for an Object

5

slide-13
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
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
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
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
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
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
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
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
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
SLIDE 22

The str String for an Object

Human interpretable strings are useful as well:

6

slide-23
SLIDE 23

The str String for an Object

Human interpretable strings are useful as well: >>> from fractions import Fraction

6

slide-24
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
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
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
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
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
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
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
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
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
SLIDE 33

Polymorphic Functions

slide-34
SLIDE 34

Polymorphic Functions

8

slide-35
SLIDE 35

Polymorphic Functions

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

8

slide-36
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
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
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
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
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
SLIDE 41

Implementing repr and str

9

slide-42
SLIDE 42

Implementing repr and str

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

9

slide-43
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
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
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
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
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
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
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
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
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
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
SLIDE 53

Interfaces

10

slide-54
SLIDE 54

Interfaces

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

10

slide-55
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
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
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
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
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
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
SLIDE 61

Special Method Names

slide-62
SLIDE 62

Special Method Names in Python

12

slide-63
SLIDE 63

Special Method Names in Python

12

Certain names are special because they have built-in behavior

slide-64
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
SLIDE 82

Special Methods

13

slide-83
SLIDE 83

Special Methods

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

13

slide-84
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
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
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
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
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
SLIDE 89

Generic Functions

14

slide-90
SLIDE 90

Generic Functions

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

14

slide-91
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
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
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
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
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
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

(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