python:python_misc:oo_fundamentals_with_python
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python:python_misc:oo_fundamentals_with_python [2018/08/20 20:15] – ↷ Page name changed from python:python_misc:python_oo_fundamentals to python:python_misc:oo_fundamentals_with_python mithat | python:python_misc:oo_fundamentals_with_python [2018/11/30 18:45] (current) – [Instantiation and use] mithat | ||
---|---|---|---|
Line 2: | Line 2: | ||
===== What you'll need to know ===== | ===== What you'll need to know ===== | ||
- | In what follows, we assume know basic Python programming concepts including: | + | In what follows, we assume |
* How to create and use variables. | * How to create and use variables. | ||
Line 40: | Line 40: | ||
==== Behavior ==== | ==== Behavior ==== | ||
- | A given microwave oven also has predefined **behavior**. | + | A given microwave oven also has predefined **behavior**: |
* //Push the " | * //Push the " | ||
- | * //Push the " | + | * //Push the " |
* //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 one 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 | + | 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 |
==== Encapsulation and protection ==== | ==== Encapsulation and protection ==== | ||
Line 70: | Line 70: | ||
{{: | {{: | ||
- | 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. That master | + | 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 |
- | Every Farberware 4241 begins its life identical to every other freshly-made Farberware 4241 because they were made using the same master plan. So, 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. | + | Languages that support **class-based object-oriented programming** let you define classes at a very high level. Once you've defined a class you can then create **instances**: |
- | In **class-based object-oriented programming**, | + | 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: | ||
)) \\ {{..: | )) \\ {{..: | ||
- | 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 // | One way to start building a model for a class is to start listing the public-facing behavior (or the // | ||
Line 98: | Line 98: | ||
* //reset//: sets the count to zero. | * //reset//: sets the count to zero. | ||
- | Next we can think about what data we'll need to keep track of the state of a clicker-counter. In this case, it's pretty simple: all we really need is one integer to store the count value. | + | Next we can think about what data we'll need to keep track of the state of a clicker-counter. In this case, it's pretty simple: all we really need is one integer to store the //count// value. |
So, a summary of what we need so far is: | So, a summary of what we need so far is: | ||
Line 104: | Line 104: | ||
* a //click// operation | * a //click// operation | ||
* a //reset// operation | * a //reset// operation | ||
- | * an integer to store the count | + | * an integer to store the //count// |
In Python, object **attributes**, | In Python, object **attributes**, | ||
Line 126: | Line 126: | ||
</ | </ | ||
- | Class definitions follow the same header/ | + | Class definitions follow the same header/ |
- | The suite of the class definition nests additional compound statements, in this case a set of function definitions---the instance methods. | + | The suite of the class definition nests additional compound statements: function definitions |
In this class definition, we define three instance methods: '' | In this class definition, we define three instance methods: '' | ||
Line 135: | Line 135: | ||
<WRAP center round tip 80%> | <WRAP center round tip 80%> | ||
- | A common error is to forget to use '' | + | A common error is to forget to use '' |
</ | </ | ||
Line 212: | Line 212: | ||
a.click(self) | a.click(self) | ||
</ | </ | ||
- | Python' | + | Python' |
==== 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' | + | This just scratches the surface of what's possible with object-oriented programming. In particular, we haven' |
Copyright © 2011-2018 Mithat Konar. All rights reserved. | Copyright © 2011-2018 Mithat Konar. All rights reserved. |
python/python_misc/oo_fundamentals_with_python.1534796127.txt.gz · Last modified: 2018/08/20 20:15 by mithat