object oriented programming - enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 enthought, inc....

11
© 2008-2016 Enthought, Inc. 107 Object Oriented Programming

Upload: others

Post on 19-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 107

Object Oriented Programming

Page 2: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 108

A reusable collection of behavior and data •  Definition •  Subclasses •  Properties

Classes

Page 3: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 109

Modeling a Power Plant

# Data that describes the # modeled "object" name: Comanche Peak maximum_output: 2.3 Gigawatts current_output: ? Gigawatts status: normal night_watchman: Homer Simpson (!) ...

ATTRIBUTES

BEHAVIORS (METHODS) # tasks or operations that the # model does. start_up() shut_down() emergency_shutdown() ...

Page 4: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 110

Working with Objects # Our befuddled friend. >>> homer = Person(first='Homer', last='Simpson') # Create the power plant he is in charge of... >>> plant = PowerPlant(name='Comanche Peak', maximum_output=2.3, night_watchman=homer) # Start the plant using the 'start_up' method. >>> plant.start_up() # Homer does his thing. >>> homer.eat('donut') >>> homer.take_nap() # Check the status (an attribute) of # the plant. No Bueno. >>> if plant.status != 'normal': ... print 'status:', plant.status ... homer.speak('Doh!') ... plant.emergency_shutdown() status: meltdown Homer says 'Doh!'

Page 5: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 111

Class Definition class PowerPlant(object): # Constructor method def __init__(self, # self is ALWAYS the first argument. name='', maximum_output=0.0, night_watchman=None): # assign passed-in arguments to new object self.name = name self.maximum_output = maximum_output self.night_watchman = night_watchman # Intialize other attributes. self.current_output = 0.0 self.status = 'normal' # class methods def start_up(self): self.start_reactor_cooling_pump() self.reduce_boric_acid_concentration() self.remove_control_rods() def shut_down(self): ... <other methods>

Page 6: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 112

Subclasses and Inheritance

PowerPlant

NuclearPowerPlant HydroPowerPlant

Page 7: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 113

Base Class class PowerPlant(object): # Constructor method def __init__(self, # "self" is ALWAYS the first argument. name='', maximum_output=0.0, night_watchman=None): # assign passed-in arguments to new object self.name = name self.maximum_output = maximum_output self.night_watchman = night_watchman # Initialize other attributes. self.current_output = 0.0 self.status = 'normal' # Default implementation for methods def start_up(self): self.current_output = self.maximum_output def shut_down(self): self.current_output = 0.0 ...

Page 8: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 114

Sub-Classing # Derive 'specialized' classes from the PowerPlant base class. # They will 'inherit' the methods of the base class. class NuclearPowerPlant(PowerPlant): # over-ride methods that need custom behavior. def start_up(self): self.start_reactor_cooling_pump() self.reduce_boric_acid_concentration() self.remove_control_rods() def shut_down(self): ...

Page 9: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 115

Using Super # Use 'super' to call the "super-class" (parent class) methods. class HydroPowerPlant(PowerPlant): def __init__(self, name='', river_name='', maximum_output=0.0, night_watchman=None): # Use 'super' to call an over-ridden method from the # base class. super(HydroPowerPlant, self).__init__(name=name, maximum_output=maximum_output, night_watchman=night_watchman) self.river_name = river_name # over-ride methods that need custom behavior. def start_up(self): self.open_sluice_gate() self.unlock_turbine_shaft() self.remove_control_rods()

Page 10: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 116

Interchangeable Classes

# The same code will work without modification for a # NuclearPowerPlant or a HydroPowerPlant. >>> homer = Person(first='Homer', last='Simpson') # Create the power plant he is in charge of... >>> plant = HydroPowerPlant(name='Comanche Peak', maximum_output=2.3, night_watchman=homer) # Start the plant using the 'start_up' method. >>> plant.start_up() # Homer does his thing. >>> homer.eat('donut') >>> homer.take_nap() # Check the status (an attribute) of the plant. No Bueno. >>> if plant.status != 'normal': ... print 'status:', plant.status ... homer.speak('Doh!') ... plant.emergency_shutdown() status: Fish stuck in impeller. Homer says 'Doh!'

Page 11: Object Oriented Programming - Enthoughtmmckerns/p4se/files/oop.pdf · © 2008-2016 Enthought, Inc. 109 Modeling a Power Plant # Data that describes the # modeled "object" name: Comanche

© 2008-2016 Enthought, Inc. 125

Class Scoping Rules

# global variable var = 0 class MyClass(object): # class variable var = 1 def access_class_c(self): print 'class var:', self.var def access_global_c(self): print 'global var:', var def write_class_c(self): # Modify the class variable. MyClass.var = 2 print 'class var:', self.var def write_instance_c(self): # Create an instance variable. self.var = 3 print 'instance var:', self.var

See demo/variable_scoping/class_scoping.py.

>>> obj = MyClass() >>> obj.access_class_c() class var: 1 >>> obj.access_global_c() global var: 0 >>> obj.write_class_c() class var: 2 >>> obj.write_instance_c() instance var: 3

CLASS SCOPING EXAMPLES