Variable
Private variables:
If you want the internal properties to be outside access, you can add the name of the property with two underscores __
, in Python, the variable name of the instance is changed to __
a private variable (private), which can only be accessed internally and cannot be accessed externally;
Special variables:
It is important to note that in Python, the variable name is similar, which starts with a double underscore, and ends with a double underscore, which is a special variable that __xxx__
can be accessed directly, not a private variable, so __name__
__score__
the variable name cannot be used.
A variable that starts with a single underline:
There are times when you see an instance variable name that starts with an underscore, such as an _name
instance variable that can be accessed externally, but, as you see in the rules, when you look at a variable like this, the meaning is, "although I can be accessed, please treat me as a private variable and don't feel free to access it."
is an instance variable that starts with a double underscore not necessarily externally accessible? Actually, it's not. cannot be accessed directly __name
because the Python interpreter has changed the variable to the outside __name
_Student__name
, so you can still _Student__name
access the __name
variable by:
Inheritance
When both the subclass and the parent class have the same run()
method, we say that the child class run()
overrides the parent class, and the subclass is run()
always called when the code is running run()
. In this way, we gain another benefit of inheritance: polymorphism.
Class Animal (object): def run (self): print (' Animal is running ... ')
Overriding the parent class method and adding methods
Class Dog (Animal): def run: print (' Dog is running ... ') def Eat (self): print (' Eating meat ... ')
To understand what polymorphism is, let's start with a little more explanation of the data type. When we define a class, we actually define a data type. The data types we define are the same as the data types that python comes with, such as STR, list, Dict.
To understand the benefits of polymorphism, we also need to write a function that takes a Animal
variable of one type:
def run_twice(animal): animal.run() animal.run()
When we pass Animal
in an instance, we run_twice()
print out:
>>> run_twice(Animal())Animal is running...Animal is running...
When we pass Dog
in an instance, we run_twice()
print out:
>>> run_twice(Dog())Dog is running...Dog is running...
When we pass Cat
in an instance, we run_twice()
print out:
>>> run_twice(Cat())Cat is running...Cat is running...
You will find that a new subclass is not Animal
necessary to run_twice()
make any changes, in fact, any Animal
function or method that relies on parameters can work without modification, because of polymorphism.
The advantage of polymorphism is that when we need to pass in Dog
, Cat
Tortoise
... , we just need to receive the Animal
type, because,, Dog
Cat
Tortoise
... Are all Animal
types, and then Animal
you can do so by type. Because the Animal
type has run()
methods, any type passed in, as long as it is a Animal
class or subclass, will automatically invoke the actual type of run()
method, which is the meaning of polymorphism:
For a variable, we just need to know that it is a Animal
type, without knowing exactly what its subtype is, you can safely invoke run()
the method, and the method that is called is the function of the, run()
or the Animal
Dog
Cat
Tortoise
object, Determined by the exact type of the object at run time, this is the true power of polymorphism: The caller just calls, regardless of the details, and when we add a Animal
subclass, simply make sure that the run()
method is written correctly, regardless of how the original code is called. This is the famous "opening and shutting" principle:
Open to extensions: Allow new Animal
subclasses;
Closed for modification: You do not need to modify Animal
run_twice()
such functions as dependent types.
Inheritance can also be inherited from the first level, like from Grandpa to father, and then to the son of the relationship. And any class, in the end, can be traced back to the root class object, which looks like a backward tree. For example, the following inheritance tree:
Static language vs Dynamic Language
For a static language, such as Java, if an incoming Animal
type is required, the incoming object must be a Animal
type or its subclass, otherwise the method cannot be called run()
.
For dynamic languages such as Python, the incoming type is not necessarily required Animal
. We just need to make sure that the incoming object has one run()
way to do it:
class Timer(object): def run(self): print(‘Start...‘)
This is the "duck type" of dynamic language, it does not require strict inheritance system, an object as long as "look like a duck, walk like a duck", it can be regarded as a duck.
Python's "File-like object" is a type of duck. For a real file object, it has a read()
method that returns its contents. However, many objects, as long as there are read()
methods, are considered "File-like object". Many functions receive the parameter "File-like object", you do not have to pass in the real file object, can pass any object that implements the read()
method completely.
Classes in Python