Object Oriented Programming Revisited Classes Ideal for modeling

  • Slides: 14
Download presentation
Object Oriented Programming Revisited • Classes – – Ideal for modeling real-world phenomena Encapsulate

Object Oriented Programming Revisited • Classes – – Ideal for modeling real-world phenomena Encapsulate data: Attributes Encapsulate function: Behaviors Act as blueprints • Objects are instantiated from classes 1

An abstract game with two players • Player: – – Name Overall skill (5.

An abstract game with two players • Player: – – Name Overall skill (5. 0 – 10. 0) Motivation flux mf (0. 0 – 1. 0) Current strength: • Current motivation cm : random number in [ -mf, mf ] • Current strength = skill + skill * cm With motivation 1. 0, current strength is 2 * skill. With motivation 0. 0, current strength is skill. Motivation flux 0. 0: very stable player. Motivation flux 1. 0: very unstable player. 2

player. py • Object reference self first argument in all methods • self refers

player. py • Object reference self first argument in all methods • self refers to the object on which the method is called • Object attribute self. skill vs. local variable skill 3

>>> >>> 7. 5 No self argument in method calls! from player import Player

>>> >>> 7. 5 No self argument in method calls! from player import Player john = Player('John', 1. 0, 8. 0) paul = Player('Paul') print paul. skill john Python puts object reference in place of self paul name = ‘John’ name = ‘Paul’ skill = 8. 0 skill = 7. 5 motivation_flux = 1. 0 motivation_flux = 0. 0 class Player 4

Summary – Class header • Keyword class begins definition, followed by name of class

Summary – Class header • Keyword class begins definition, followed by name of class and colon (: ) – Body of class • Indented block of code – Optional documentation string • Describes the class • Appears immediately after class header – Constructor method __init__ • Executes each time an object is created • Initialize attributes of class • Returns None – Object reference • All methods must at least specify this one parameter • Represents object of class on which a method is called • Called self by convention 5

Remember the self. prefix when referencing object attributes >>> from player_wrong import Player >>>

Remember the self. prefix when referencing object attributes >>> from player_wrong import Player >>> ringo = Player("Ringo") >>> print ringo. my_skill Traceback (most recent call last): File "<stdin>", line 1, in ? Attribute. Error: Player instance has no attribute ‘my_skill' 6

player 2. py Let’s do something with our objects: method play >>> from player

player 2. py Let’s do something with our objects: method play >>> from player 2 import Player >>> john = Player("John", 1. 0, 8. 0) >>> paul = Player("Paul") >>> john. play(paul) John (11. 97) - Paul (7. 50): John wins >>> john. play(paul) John (1. 88) - Paul (7. 50): Paul wins >>> paul. play(john) Paul (7. 50) - John (3. 32): Paul wins 7

Destructors • Constructor named __init__ • Destructor – Method is named __del__ – Executed

Destructors • Constructor named __init__ • Destructor – Method is named __del__ – Executed automatically when object is destroyed or when no more references to object exist – Can be used for housekeeping before Python reclaims object memory 8

Class Attributes • Class Attributes – One copy of attribute shared by all objects

Class Attributes • Class Attributes – One copy of attribute shared by all objects of a class – Represents “class-wide” information • Property of the class, not of an object of the class – Initialized once in the class definition (not in constructor) • Otherwise it would be re-initialized every time an object is created – Accessed through the class name or any object of the class • May exist even if no objects of class exist 9

player 3. py Adding class attribute count + destructor >>> from player 3 import

player 3. py Adding class attribute count + destructor >>> from player 3 import Player >>> paul = Player('Paul') >>> ringo = Player('Ringo') >>> del paul Player Paul deleted 10

Preparing to hold tournaments player 4. py • New object attribute wins to keep

Preparing to hold tournaments player 4. py • New object attribute wins to keep track of match results • New play method which updates attribute wins 11

Testing. . >>> from player 4 import Player >>> john = Player('John', 0. 2,

Testing. . >>> from player 4 import Player >>> john = Player('John', 0. 2, 8. 0) >>> paul = Player('Paul') >>> john. play(paul) John defeats Paul >>> john. play(paul) Paul defeats John >>> print john. wins 3 >>> print paul. wins 1 >>> ^D Player Paul deleted Player John deleted threonine: ~/python% NB: destructor called automatically when session ends 12

player_game. py Now we’re ready to hold a tournament! 13

player_game. py Now we’re ready to hold a tournament! 13

Holding tournament with four players threonine: ~/python% python player_game. py Player name: John's skill

Holding tournament with four players threonine: ~/python% python player_game. py Player name: John's skill (5. 0 -10. 0): 8 John's motivation flux (0. 0 -1. 0): 0. 2 Player name: Paul's skill (5. 0 -10. 0): 7. 5 Paul's motivation flux (0. 0 -1. 0): 0 Player name: George's skill (5. 0 -10. 0): 7. 5 George's motivation flux (0. 0 -1. 0): 0. 5 Player name: Ringo's skill (5. 0 -10. 0): 7. 5 Ringo's motivation flux (0. 0 -1. 0): 1. 0 Paul defeats John defeats George Ringo defeats John George defeats Paul Ringo defeats George Ringo is the winner with 3 wins! Player initialization Tournament matches 14