I just have learning Object Oriented Programming by reading the following book, and speculating some pieces of code.
“Python 3 Object-Oriented Programming: Build robust and maintainable software with object-oriented design patterns in Python 3.8, 3rd Edition (English Edition)”
#1 The Meaning to Be Object-Oriented
The book says:
object is a collection of data and associated behaviors. …. object-oriented means functionally directed toward modeling objects. This is one of many techniques used for modeling complex systems. It is defined by describing a collection of interacting objects via their data and behavior. … Object-oriented programming (OOP) is the process of converting this perfectly-defined design into a working program that does exactly what the CEO originally requested.
sources; “Introducing object-oriented”, No. 375/8757
#2 The Role of Class
Classes describe objects.
sources; “Objects and classes”, No. 398/8757
#3 How to Classes Describe Objects
By Adding Methods .
… methods are like functions in structured programming, but they magically have access to all the data associated with this object. Like functions, methods can also accept parameters and return values.
No. 473/8757
##3–1 What is the difference between an argument and a parameter?
A “parameter” is a very general, broad thing, but an “argument: is a very specific, concrete thing. This is best illustrated via everyday examples:
Example 1: Vending Machines — Money is the parameter, $2.00 is the argument
Most machines take an input and return an output. For example a vending machine takes as an input: money, and returns: fizzy drinks as the output. In that particular case, it accepts as a parameter: money.
What then is the argument? Well if I put $2.00 into the machine, then the argument is: $2.00 — it is the very specific input used.
sources;
“What’s the difference between an argument and a parameter?”
#4 How to Define Class
- Class definition starts with <your class keyword>
- It is terminated with a colon.
class Hand:
pairs = {
‘rock’: ‘scissor’,
‘scissor’: ‘paper’,
‘paper’: ‘rock’
}def __init__(self, pair):
if pair in self.pairs:
self.pair = pair
else:
raise ValueError(
“It’s rock, paper, scissor… Not rock, {},
scissor.”.format(kind)
)
try_conts = 0
objs = []while True:
if try_conts <= 2:
objs.append(Hand("rock"))
try_conts += 1 else:
breakfor i, obj in enumerate(objs):
print('---' + str(i) + '------------')
print('Hand: {}'.format(obj))
print('obj.__dict__: {}'.format(obj.__dict__))
print('obj.kind: {}'.format(obj.kind))
The output is bellow;
---0------------
Hand: <__main__.Hand object at 0x7fdba6982a58>
obj.__dict__: {'kind': 'rock'}
obj.kind: rock
---1------------
Hand: <__main__.Hand object at 0x7fdba6982ac8>
obj.__dict__: {'kind': 'rock'}
obj.kind: rock
---2------------
Hand: <__main__.Hand object at 0x7fdba69137b8>
obj.__dict__: {'kind': 'rock'}
obj.kind: rock
At first glance, it looks like all the objs are the same…
No!!
All of them are quite different from one another.
if objs[0] == objs[1] == objs[2]:
print("objs[0] == objs[1] == objs[2]: {}".format(objs[0] == objs[1] == objs[2]))
else:
print("objs[0] == objs[1] == objs[2]: {}".format(objs[0] == objs[1] == objs[2]))
The output is bellow;
objs[0] == objs[1] == objs[2]: False
#5 How to Specify the Complete Path to the Module, Function, or Class
Each person has their own format, and there are no official rules.
When a directory structure is bellow, the author recommended us the following format:
parent_directory/
main.py
ecommerce/
__init__.py
database.py
products.py
payments/
__init__.py
square.py
stripe.py
from ecommerce import productsproduct = products.Product()
His reason he recommended is to avoid conflict.
When we use a double underscore, the property is prefixed with _<classname>. When methods in the class internally access the variable, they are automatically unmangled. When external classes wish to access it, they have to do the name mangling themselves. So, name mangling does not guarantee privacy; it only strongly recommends it.