User Tools

Site Tools


python:python_misc:oo_fundamentals_with_python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python:python_misc:oo_fundamentals_with_python [2018/08/23 17:08] – [Class-based object-orientation] mithatpython:python_misc:oo_fundamentals_with_python [2018/11/30 18:45] (current) – [Instantiation and use] mithat
Line 40: Line 40:
 ==== Behavior ==== ==== Behavior ====
  
-A given microwave oven also has predefined **behavior**.+A given microwave oven also has predefined **behavior**:
  
   * //Push the "1" button// → start cooking at maximum power for one minute.   * //Push the "1" button// → start cooking at maximum power for one minute.
-  * //Push the "+30sec" button// → add 30 seconds to the cooking time if it's already cooking or start cooking for 30 seconds at maximum power otherwise.+  * //Push the "+30sec" button// → start cooking for 30 seconds at maximum power or add 30 seconds to the cooking time if it's already cooking.
   * //Push a magical combination of buttons// → set the internal clock.   * //Push a magical combination of buttons// → set the internal clock.
   * And so on.   * And so on.
  
-These operations are ones that happen to be public-facing (i.e., operations a user can engage). There might very well also be operations that go on inside the oven to support the oven's functioning that the user will never be aware of. All these operations collectively define the oven'behavior. We call the public-facing behavior (i.e., the operations a user can engage) the oven's **interface**.+These operations are ones that happen to be public-facing (i.e., operations a user can engage). There might very well also be operations that go on inside the oven that the user will never be aware of to support the oven'functioning. We call the //public-facing behavior// (i.e., the operations a user can engage) the oven's **interface**.
  
 ====  Encapsulation and protection ==== ====  Encapsulation and protection ====
Line 70: Line 70:
 {{:python:about_python:farberware4241-200.jpeg?nolink|}} {{:python:about_python:farberware4241-200.jpeg?nolink|}}
  
-I have a Farberware 4241 microwave oven in my office. It's small, but it gets the job done. There is a factory somewhere making these by the thousand million, and they make all the Farberware 4241s from a master plan. That master plan defines what the Farberware 4241 //is//. In computing terminology, we would call that master plan a **class**. A class contains all the specifications needed to make a particular kind of object. +I have a Farberware 4241 microwave oven in my office. It's small, but it gets the job done. There is a factory somewhere making these by the thousand million. They make all the Farberware 4241s from a master plan, a plan that defines what the Farberware 4241 //is//. In object-oriented design terminology, we would call that master plan a **class**. A class contains all the specifications needed to make a particular kind of object.
  
-Every Farberware 4241 begins its life identical to every other freshly-made Farberware 4241 because they were made using the same master planSo, the one in my office that I pulled out of the box last month was identical to the one that I bought my best friend last week later because they were built from the same master plan, or, if you will, the same class. But as my best friend and I started using our ovens, the state of each changed. Note that while the behavior designed into the oven---the operations it's capable of---doesn't (and won't) change, the state of any individual oven does change over time.+Languages that support **class-based object-oriented programming** let you define classes at a very high levelOnce you've defined a class you can then create **instances**: objects created from a class. The process of creating an instance from a class is called **instantiation**.
  
-In **class-based object-oriented programming**, we call objects that have been created from classes **instances**. Thus my oven is an instance of a Farberware 4241. The process of creating an instance from a class is called **instantiation**. So you might say the Farberware factory spends it's entire day instantiating 4241 ovens.+Thus my oven is an instance of a Farberware 4241, and you might say the Farberware factory spends it's entire day instantiating 4241 ovens.
  
 ==== Prototype-based object-orientation ==== ==== Prototype-based object-orientation ====
Line 91: Line 91:
 )) \\ {{..:about_python:tottycounter.jpg?234|}} )) \\ {{..:about_python:tottycounter.jpg?234|}}
  
-In case you've never seen this bit of advanced technology before, it's a clicker-counter or tally-counter. It has two controls: a button you click to advance the counter by one and another you press to reset the count to zero. Our goal is to build one of these in software using object-orientation.+In case you've never seen one of these bits of advanced technology before, it's a clicker-counter or tally-counter. It has two controls: a button you click to advance the counter by one and another you press to reset the count to zero. Our goal is to build one of these in software using object-orientation.
  
 One way to start building a model for a class is to start listing the public-facing behavior (or the //interface//) you want an object of that class to have. A pretty comprehensive list of the things you might do with a clicker-counter is: One way to start building a model for a class is to start listing the public-facing behavior (or the //interface//) you want an object of that class to have. A pretty comprehensive list of the things you might do with a clicker-counter is:
Line 212: Line 212:
 a.click(self) a.click(self)
 </code> </code>
-Python's inner workings manage the fist parameter automatically and implicitly.+Python's inner workings manage the fist parameter (i.e., ''self''automatically and implicitly.
  
 ==== Parameterized constructors and methods ==== ==== Parameterized constructors and methods ====
Line 267: Line 267:
 ==== Next steps ==== ==== Next steps ====
  
-This just scratches the surface of what's possible with object-oriented programming. In particular, we haven't addressed the matter of protecting our class' members. But with even with the basics you have learned here, you can now create Python objects whose state is maintained through predefined behaviors.+This just scratches the surface of what's possible with object-oriented programming. In particular, we haven't addressed the matter of protecting our class' members or reusing code through //inheritance//. But with even with the basics you have learned here, you can now create Python objects whose state is maintained through predefined behaviors.
  
 Copyright © 2011-2018 Mithat Konar. All rights reserved. Copyright © 2011-2018 Mithat Konar. All rights reserved.
python/python_misc/oo_fundamentals_with_python.1535044126.txt.gz · Last modified: 2018/08/23 17:08 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki