61A Extra Lecture 6 Implementing an Object System 3 Implementing - - PowerPoint PPT Presentation

61a extra lecture 6 implementing an object system
SMART_READER_LITE
LIVE PREVIEW

61A Extra Lecture 6 Implementing an Object System 3 Implementing - - PowerPoint PPT Presentation

61A Extra Lecture 6 Implementing an Object System 3 Implementing an Object System Today's topics: 3 Implementing an Object System Today's topics: What is a class? 3 Implementing an Object System Today's topics: What is a class?


slide-1
SLIDE 1

61A Extra Lecture 6

slide-2
SLIDE 2

Implementing an Object System

3

slide-3
SLIDE 3

Implementing an Object System

Today's topics:

3

slide-4
SLIDE 4

Implementing an Object System

Today's topics:

  • What is a class?

3

slide-5
SLIDE 5

Implementing an Object System

Today's topics:

  • What is a class?
  • What is an instance?

3

slide-6
SLIDE 6

Implementing an Object System

Today's topics:

  • What is a class?
  • What is an instance?
  • How do we create inheritance relationships?

3

slide-7
SLIDE 7

Implementing an Object System

Today's topics:

  • What is a class?
  • What is an instance?
  • How do we create inheritance relationships?
  • How do we write code for attribute look-up procedures?

3

slide-8
SLIDE 8

Implementing an Object System

Today's topics:

  • What is a class?
  • What is an instance?
  • How do we create inheritance relationships?
  • How do we write code for attribute look-up procedures?

3

Tools we'll use:

slide-9
SLIDE 9

Implementing an Object System

Today's topics:

  • What is a class?
  • What is an instance?
  • How do we create inheritance relationships?
  • How do we write code for attribute look-up procedures?

3

Tools we'll use:

  • Dispatch dictionaries
slide-10
SLIDE 10

Implementing an Object System

Today's topics:

  • What is a class?
  • What is an instance?
  • How do we create inheritance relationships?
  • How do we write code for attribute look-up procedures?

3

Tools we'll use:

  • Dispatch dictionaries
  • Higher-order functions
slide-11
SLIDE 11

The OOP Abstraction Barrier (a.k.a. the Line)

4

slide-12
SLIDE 12

The OOP Abstraction Barrier (a.k.a. the Line)

4

THE LINE

slide-13
SLIDE 13

The OOP Abstraction Barrier (a.k.a. the Line)

Above the Line:

4

THE LINE

slide-14
SLIDE 14

The OOP Abstraction Barrier (a.k.a. the Line)

Above the Line:

  • Objects with local state & interact via message passing

4

THE LINE

slide-15
SLIDE 15

The OOP Abstraction Barrier (a.k.a. the Line)

Above the Line:

  • Objects with local state & interact via message passing
  • Objects are instantiated by classes, which are also objects

4

THE LINE

slide-16
SLIDE 16

The OOP Abstraction Barrier (a.k.a. the Line)

Above the Line:

  • Objects with local state & interact via message passing
  • Objects are instantiated by classes, which are also objects
  • Classes may inherit from other classes to share behavior

4

THE LINE

slide-17
SLIDE 17

The OOP Abstraction Barrier (a.k.a. the Line)

Above the Line:

  • Objects with local state & interact via message passing
  • Objects are instantiated by classes, which are also objects
  • Classes may inherit from other classes to share behavior
  • Mechanics of objects are governed by "evaluation procedures"

4

THE LINE

slide-18
SLIDE 18

The OOP Abstraction Barrier (a.k.a. the Line)

Above the Line:

  • Objects with local state & interact via message passing
  • Objects are instantiated by classes, which are also objects
  • Classes may inherit from other classes to share behavior
  • Mechanics of objects are governed by "evaluation procedures"

4

Below the Line: THE LINE

slide-19
SLIDE 19

The OOP Abstraction Barrier (a.k.a. the Line)

Above the Line:

  • Objects with local state & interact via message passing
  • Objects are instantiated by classes, which are also objects
  • Classes may inherit from other classes to share behavior
  • Mechanics of objects are governed by "evaluation procedures"

4

Below the Line:

  • Objects have mutable dictionaries of attributes

THE LINE

slide-20
SLIDE 20

The OOP Abstraction Barrier (a.k.a. the Line)

Above the Line:

  • Objects with local state & interact via message passing
  • Objects are instantiated by classes, which are also objects
  • Classes may inherit from other classes to share behavior
  • Mechanics of objects are governed by "evaluation procedures"

4

Below the Line:

  • Objects have mutable dictionaries of attributes
  • Attribute look-up for instances is a function

THE LINE

slide-21
SLIDE 21

The OOP Abstraction Barrier (a.k.a. the Line)

Above the Line:

  • Objects with local state & interact via message passing
  • Objects are instantiated by classes, which are also objects
  • Classes may inherit from other classes to share behavior
  • Mechanics of objects are governed by "evaluation procedures"

4

Below the Line:

  • Objects have mutable dictionaries of attributes
  • Attribute look-up for instances is a function
  • Attribute look-up for classes is another function

THE LINE

slide-22
SLIDE 22

The OOP Abstraction Barrier (a.k.a. the Line)

Above the Line:

  • Objects with local state & interact via message passing
  • Objects are instantiated by classes, which are also objects
  • Classes may inherit from other classes to share behavior
  • Mechanics of objects are governed by "evaluation procedures"

4

Below the Line:

  • Objects have mutable dictionaries of attributes
  • Attribute look-up for instances is a function
  • Attribute look-up for classes is another function
  • Object instantiation is another function

THE LINE

slide-23
SLIDE 23

Implementing the Object Abstraction

5

slide-24
SLIDE 24

Implementing the Object Abstraction

Fundamental OOP concepts:

5

slide-25
SLIDE 25

Implementing the Object Abstraction

Fundamental OOP concepts:

  • Object instantiation and initialization

5

slide-26
SLIDE 26

Implementing the Object Abstraction

Fundamental OOP concepts:

  • Object instantiation and initialization
  • Attribute look-up and assignment

5

slide-27
SLIDE 27

Implementing the Object Abstraction

Fundamental OOP concepts:

  • Object instantiation and initialization
  • Attribute look-up and assignment
  • Method invocation

5

slide-28
SLIDE 28

Implementing the Object Abstraction

Fundamental OOP concepts:

  • Object instantiation and initialization
  • Attribute look-up and assignment
  • Method invocation
  • Inheritance

5

slide-29
SLIDE 29

Implementing the Object Abstraction

Fundamental OOP concepts:

  • Object instantiation and initialization
  • Attribute look-up and assignment
  • Method invocation
  • Inheritance

5

Not-so-fundamental issues (that we'll skip):

slide-30
SLIDE 30

Implementing the Object Abstraction

Fundamental OOP concepts:

  • Object instantiation and initialization
  • Attribute look-up and assignment
  • Method invocation
  • Inheritance

5

Not-so-fundamental issues (that we'll skip):

  • Dot expression syntax
slide-31
SLIDE 31

Implementing the Object Abstraction

Fundamental OOP concepts:

  • Object instantiation and initialization
  • Attribute look-up and assignment
  • Method invocation
  • Inheritance

5

Not-so-fundamental issues (that we'll skip):

  • Dot expression syntax
  • Multiple inheritance (on your homework)
slide-32
SLIDE 32

Implementing the Object Abstraction

Fundamental OOP concepts:

  • Object instantiation and initialization
  • Attribute look-up and assignment
  • Method invocation
  • Inheritance

5

Not-so-fundamental issues (that we'll skip):

  • Dot expression syntax
  • Multiple inheritance (on your homework)
  • Introspection (e.g., what class does this object have?)
slide-33
SLIDE 33

Instances

6

slide-34
SLIDE 34

Instances

Dispatch dictionary with messages 'get' and 'set'

6

slide-35
SLIDE 35

Instances

Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes"

6

slide-36
SLIDE 36

Instances

Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes"

6

(Demo)

slide-37
SLIDE 37

Instances

Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes"

6

def make_instance(cls): """Return a new object instance.""" def get_value(name): if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) def set_value(name, value): attributes[name] = value attributes = {} instance = {'get': get_value, 'set': set_value} return instance (Demo)

slide-38
SLIDE 38

Instances

Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes"

6

def make_instance(cls): """Return a new object instance.""" def get_value(name): if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) def set_value(name, value): attributes[name] = value attributes = {} instance = {'get': get_value, 'set': set_value} return instance The class of the instance (Demo)

slide-39
SLIDE 39

Instances

Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes"

6

def make_instance(cls): """Return a new object instance.""" def get_value(name): if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) def set_value(name, value): attributes[name] = value attributes = {} instance = {'get': get_value, 'set': set_value} return instance The class of the instance Match name against instance attributes (Demo)

slide-40
SLIDE 40

Instances

Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes"

6

def make_instance(cls): """Return a new object instance.""" def get_value(name): if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) def set_value(name, value): attributes[name] = value attributes = {} instance = {'get': get_value, 'set': set_value} return instance The class of the instance Look up the name in the class Match name against instance attributes (Demo)

slide-41
SLIDE 41

Instances

Dispatch dictionary with messages 'get' and 'set' Attributes stored in a local dictionary "attributes"

6

def make_instance(cls): """Return a new object instance.""" def get_value(name): if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) def set_value(name, value): attributes[name] = value attributes = {} instance = {'get': get_value, 'set': set_value} return instance The class of the instance Look up the name in the class Match name against instance attributes Assignment always creates/modifies instance attributes (Demo)

slide-42
SLIDE 42

Bound Methods

7

slide-43
SLIDE 43

Bound Methods

If looking up a name returns a class attribute value that is a function, getattr returns a bound method

7

slide-44
SLIDE 44

Bound Methods

If looking up a name returns a class attribute value that is a function, getattr returns a bound method

7

def make_instance(cls): def get_value(name): if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) ...

slide-45
SLIDE 45

Bound Methods

If looking up a name returns a class attribute value that is a function, getattr returns a bound method

7

def make_instance(cls): def get_value(name): if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) ...

slide-46
SLIDE 46

Bound Methods

If looking up a name returns a class attribute value that is a function, getattr returns a bound method

7

def make_instance(cls): def get_value(name): if name in attributes: return attributes[name] else: value = cls['get'](name) return bind_method(value, instance) ... (Demo)

slide-47
SLIDE 47

Classes

8

slide-48
SLIDE 48

Classes

Dispatch dictionaries with messages 'get', 'set', and 'new'

8

slide-49
SLIDE 49

Classes

Dispatch dictionaries with messages 'get', 'set', and 'new'

8

(Demo)

slide-50
SLIDE 50

Classes

Dispatch dictionaries with messages 'get', 'set', and 'new'

8

def make_class(attributes={}, base_class=None): """Return a new class.""" def get_value(name): if name in attributes: return attributes[name] elif base_class is not None: return base_class['get'](name) def set_value(name, value): attributes[name] = value def new(*args): return init_instance(cls, *args) cls = {'get': get_value, 'set': set_value, 'new': new} return cls (Demo)

slide-51
SLIDE 51

Classes

Dispatch dictionaries with messages 'get', 'set', and 'new'

8

def make_class(attributes={}, base_class=None): """Return a new class.""" def get_value(name): if name in attributes: return attributes[name] elif base_class is not None: return base_class['get'](name) def set_value(name, value): attributes[name] = value def new(*args): return init_instance(cls, *args) cls = {'get': get_value, 'set': set_value, 'new': new} return cls The class attribute look-up procedure (Demo)

slide-52
SLIDE 52

Classes

Dispatch dictionaries with messages 'get', 'set', and 'new'

8

def make_class(attributes={}, base_class=None): """Return a new class.""" def get_value(name): if name in attributes: return attributes[name] elif base_class is not None: return base_class['get'](name) def set_value(name, value): attributes[name] = value def new(*args): return init_instance(cls, *args) cls = {'get': get_value, 'set': set_value, 'new': new} return cls The class attribute look-up procedure (Demo)

slide-53
SLIDE 53

Classes

Dispatch dictionaries with messages 'get', 'set', and 'new'

8

def make_class(attributes={}, base_class=None): """Return a new class.""" def get_value(name): if name in attributes: return attributes[name] elif base_class is not None: return base_class['get'](name) def set_value(name, value): attributes[name] = value def new(*args): return init_instance(cls, *args) cls = {'get': get_value, 'set': set_value, 'new': new} return cls The class attribute look-up procedure Common dispatch dictionary pattern (Demo)

slide-54
SLIDE 54

Instantiation and Initialization

9

slide-55
SLIDE 55

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

slide-56
SLIDE 56

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

def make_class(attributes={}, base_class=None): ... def new(*args): return init_instance(cls, *args) ...

slide-57
SLIDE 57

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

def make_class(attributes={}, base_class=None): ... def new(*args): return init_instance(cls, *args) ... def init_instance(cls, *args):

slide-58
SLIDE 58

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

def make_class(attributes={}, base_class=None): ... def new(*args): return init_instance(cls, *args) ... def init_instance(cls, *args): """Return a new instance of cls, initialized with args."""

slide-59
SLIDE 59

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

def make_class(attributes={}, base_class=None): ... def new(*args): return init_instance(cls, *args) ... def init_instance(cls, *args): """Return a new instance of cls, initialized with args.""" instance = make_instance(cls)

slide-60
SLIDE 60

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

def make_class(attributes={}, base_class=None): ... def new(*args): return init_instance(cls, *args) ... def init_instance(cls, *args): """Return a new instance of cls, initialized with args.""" instance = make_instance(cls) Dispatch dictionary

slide-61
SLIDE 61

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

def make_class(attributes={}, base_class=None): ... def new(*args): return init_instance(cls, *args) ... def init_instance(cls, *args): """Return a new instance of cls, initialized with args.""" instance = make_instance(cls) init = cls['get']('__init__') Dispatch dictionary

slide-62
SLIDE 62

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

def make_class(attributes={}, base_class=None): ... def new(*args): return init_instance(cls, *args) ... def init_instance(cls, *args): """Return a new instance of cls, initialized with args.""" instance = make_instance(cls) init = cls['get']('__init__') The constructor name is fixed here Dispatch dictionary

slide-63
SLIDE 63

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

def make_class(attributes={}, base_class=None): ... def new(*args): return init_instance(cls, *args) ... def init_instance(cls, *args): """Return a new instance of cls, initialized with args.""" instance = make_instance(cls) init = cls['get']('__init__') if init is not None: The constructor name is fixed here Dispatch dictionary

slide-64
SLIDE 64

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

def make_class(attributes={}, base_class=None): ... def new(*args): return init_instance(cls, *args) ... def init_instance(cls, *args): """Return a new instance of cls, initialized with args.""" instance = make_instance(cls) init = cls['get']('__init__') if init is not None: init(instance, *args) The constructor name is fixed here Dispatch dictionary

slide-65
SLIDE 65

Instantiation and Initialization

First makes a new instance, then invokes the __init__ method

9

def make_class(attributes={}, base_class=None): ... def new(*args): return init_instance(cls, *args) ... def init_instance(cls, *args): """Return a new instance of cls, initialized with args.""" instance = make_instance(cls) init = cls['get']('__init__') if init is not None: init(instance, *args) return instance The constructor name is fixed here Dispatch dictionary

slide-66
SLIDE 66

Example: Defining an Account Class

10

slide-67
SLIDE 67

Example: Defining an Account Class

10

(Demo)

slide-68
SLIDE 68

Example: Defining an Account Class

10

def make_account_class(): interest = 0.02 def __init__(self, account_holder): self['set']('holder', account_holder) self['set']('balance', 0) def deposit(self, amount): new_balance = self['get']('balance') + amount self['set']('balance', new_balance) return self['get']('balance') def withdraw(self, amount): balance = self['get']('balance') if amount > balance: return 'Insufficient funds' self['set']('balance', balance - amount) return self['get']('balance') return make_class(locals()) Account = make_account_class()

(Demo)

slide-69
SLIDE 69

Example: Defining an Account Class

10

def make_account_class(): interest = 0.02 def __init__(self, account_holder): self['set']('holder', account_holder) self['set']('balance', 0) def deposit(self, amount): new_balance = self['get']('balance') + amount self['set']('balance', new_balance) return self['get']('balance') def withdraw(self, amount): balance = self['get']('balance') if amount > balance: return 'Insufficient funds' self['set']('balance', balance - amount) return self['get']('balance') return make_class(locals()) Account = make_account_class()

(Demo)

slide-70
SLIDE 70

Example: Using the Account Class

The Account class is instantiated and stored, then messaged

11

slide-71
SLIDE 71

Example: Using the Account Class

The Account class is instantiated and stored, then messaged

11

>>> Account = make_account_class()

slide-72
SLIDE 72

Example: Using the Account Class

The Account class is instantiated and stored, then messaged

11

>>> Account = make_account_class() >>> jim_acct = Account['new']('Jim')

slide-73
SLIDE 73

Example: Using the Account Class

The Account class is instantiated and stored, then messaged

11

>>> Account = make_account_class() >>> jim_acct = Account['new']('Jim') >>> jim_acct['get']('holder') 'Jim'

slide-74
SLIDE 74

Example: Using the Account Class

The Account class is instantiated and stored, then messaged

11

>>> Account = make_account_class() >>> jim_acct = Account['new']('Jim') >>> jim_acct['get']('holder') 'Jim' >>> jim_acct['get']('interest') 0.02

slide-75
SLIDE 75

Example: Using the Account Class

The Account class is instantiated and stored, then messaged

11

>>> Account = make_account_class() >>> jim_acct = Account['new']('Jim') >>> jim_acct['get']('holder') 'Jim' >>> jim_acct['get']('interest') 0.02 >>> jim_acct['get']('deposit')(20) 20

slide-76
SLIDE 76

Example: Using the Account Class

The Account class is instantiated and stored, then messaged

11

>>> Account = make_account_class() >>> jim_acct = Account['new']('Jim') >>> jim_acct['get']('holder') 'Jim' >>> jim_acct['get']('interest') 0.02 >>> jim_acct['get']('deposit')(20) 20 >>> jim_acct['get']('withdraw')(5) 15

slide-77
SLIDE 77

Example: Using the Account Class

The Account class is instantiated and stored, then messaged

11

>>> Account = make_account_class() >>> jim_acct = Account['new']('Jim') >>> jim_acct['get']('holder') 'Jim' >>> jim_acct['get']('interest') 0.02 >>> jim_acct['get']('deposit')(20) 20 >>> jim_acct['get']('withdraw')(5) 15 How can we also use getattr and setattr style syntax?

slide-78
SLIDE 78

Class and Instance Attributes

Instance attributes and class attributes can share names

12

slide-79
SLIDE 79

Class and Instance Attributes

Instance attributes and class attributes can share names

12

>>> Account = make_account_class()

slide-80
SLIDE 80

Class and Instance Attributes

Instance attributes and class attributes can share names

12

>>> Account = make_account_class() >>> jim_acct = Account['new']('Jim')

slide-81
SLIDE 81

Class and Instance Attributes

Instance attributes and class attributes can share names

12

>>> Account = make_account_class() >>> jim_acct = Account['new']('Jim') >>> jim_acct['set']('interest', 0.08)

slide-82
SLIDE 82

Class and Instance Attributes

Instance attributes and class attributes can share names

12

>>> Account = make_account_class() >>> jim_acct = Account['new']('Jim') >>> jim_acct['set']('interest', 0.08) >>> Account['get']('interest') 0.02

slide-83
SLIDE 83

Class and Instance Attributes

Instance attributes and class attributes can share names

12

>>> Account = make_account_class() >>> jim_acct = Account['new']('Jim') >>> jim_acct['set']('interest', 0.08) >>> Account['get']('interest') 0.02 (Demo)

slide-84
SLIDE 84

Example: Using Inheritance

CheckingAccount is a special case of Account

13

slide-85
SLIDE 85

Example: Using Inheritance

CheckingAccount is a special case of Account

13

(Demo)

slide-86
SLIDE 86

Example: Using Inheritance

CheckingAccount is a special case of Account

13

def make_checking_account_class(): interest = 0.01 withdraw_fee = 1 def withdraw(self, amount): fee = self['get']('withdraw_fee') return Account['get']('withdraw')(self, amount + fee) return make_class(locals(), Account) CheckingAccount = make_checking_account_class()

(Demo)

slide-87
SLIDE 87

Example: Using Inheritance

CheckingAccount is a special case of Account

13

def make_checking_account_class(): interest = 0.01 withdraw_fee = 1 def withdraw(self, amount): fee = self['get']('withdraw_fee') return Account['get']('withdraw')(self, amount + fee) return make_class(locals(), Account) CheckingAccount = make_checking_account_class()

(Demo)

slide-88
SLIDE 88

Relationship to the Python Object System

Object attributes are stored as dictionaries Some "magic" names, __<name>__, require special handling An object has an "attribute" called __dict__ that is a dictionary of its user-defined instance attributes

14

(Demo)

slide-89
SLIDE 89

Relationship to the Python Object System

Object attributes are stored as dictionaries Some "magic" names, __<name>__, require special handling An object has an "attribute" called __dict__ that is a dictionary of its user-defined instance attributes

14

(Demo) In Python, classes have classes too The equivalent of init_instance can be customized (metaclass)