Class learning notes in Python and class learning notes in Python
Python uses an object-oriented language that supports inheritance and polymorphism;
Define a Person class:
Copy codeThe Code is as follows:
>>> Class Person:
... Def sayHello (self ):
... Print ('hello ')
...
>>> Person. sayHello (None)
Hello
>>> Person (). sayHello ()
Hello
Class methods for modifying Person
Copy codeThe Code is as follows:
>>> Def hack_sayHello (obj ):
... Print ('... hello ')
...
>>>
>>> Person. sayHello = hack_sayHello
>>> Person. sayHello (None)
... Hello
>>> Person (). sayHello ()
... Hello
>>> SayHello = Person (). sayHello
>>> SayHello ()
... Hello
Person (). sayHello is also a function, which can be assigned to variables and called directly;
Copy codeThe Code is as follows:
>>> Person. sayHello is Person (). sayHello
False
>>> Person. sayHello = Person (). sayHello
False
Person. sayHello and Person (). sayhello is not the same object. intuitively, It is Person (). sayHello Associates (binds) A Person instance, while Person. sayHello is a class method;
The self parameter is actually the difference between a method and a function: A method binds their first parameter to its instance, so this parameter does not have to be provided;
Copy codeThe Code is as follows:
>>> Class Person:
... Name = 'unkown'
... Def sayHello (self ):
... Print ('I \ 'M' + name)
...
>>>
>>> Person. sayHello (None)
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
NameError: name 'name' is not defined
>>> P = Person ()
>>> P. name = 'wyj'
>>> P. sayHello ()
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
NameError: name 'name' is not defined
It can be seen that Python searches local scope/global scope by default when parsing variables;
Copy codeThe Code is as follows:
>>> Class Person:
... Name = 'unkown'
... Def sayHello (self ):
... Print ('I \ 'M' + self. name)
...
>>>
>>> Person. sayHello (None)
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in sayHello
AttributeError: 'nonetype 'object has no attribute 'name'
>>> P = Person ()
>>> P. name = 'wyj'
>>> P. sayHello ()
I'm wyj
All access members must use self. Can I call Person. sayHello (obj) with an object containing the name attribute?
Copy codeThe Code is as follows:
>>> Class Cat:
... Name = 'huanhuanc'
...
>>> Person. sayHello (Cat ())
I'm huanhuan
Yes. Python does not limit the use of instance objects of the same class as parameters to call class methods (it seems that Python's class mechanism is similar to Javascript );
Access Control
Python does not directly support private access, but depends on the programmer.
However, you can add a Double underline before the attribute name to give it private access (invisible to the outside world );
Copy codeThe Code is as follows:
>>> Class Person:
... Def _ private_method (self ):
... Print ('private ')
... Def test (self ):
... Self. _ private_method ()
...
>>> Person (). test ()
Private
>>> Person (). _ private_method ()
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
AttributeError: 'person 'object has no attribute '_ private_method'
In fact, all the above underlined methods have a _ ClassName _ methodName method.
Copy codeThe Code is as follows:
>>> Person. _ Person _ private_method
<Function Person. _ private_method at 0x7fed431a2d90>
Call
Copy codeThe Code is as follows:
>>> Person. _ Person _ private_method (None)
Private
In short, Python cannot prevent method calls from outside the class;
Class attributes and Object Attributes
First, you can add attributes to the class, and the new object will get a copy of the attributes.
Copy codeThe Code is as follows:
>>> Person. age = 3
>>> Person (). age
3
>>> Person. age = 4
>>> Person (). age
4
>>> P = Person ()
>>> Person. age = 31
>>> P. age
31
Modification to class attributes reflects the attributes of the previously generated object. This indicates that class attributes share a value with the attributes of the object;
Copy codeThe Code is as follows:
>>> P. age = 34
>>> P. age
34
>>> Person. age
31
>>> Person. age = 99
>>> P. age
34
Once an object's attributes are modified, the object attributes have their own values and are not reflected in class attributes, it is no longer reflected in the attributes of this object;
This behavior is similar to that of Javascript.
How to Understand the instances of class objects in python programming?
Class is the generalization of a class of things, such as people.
The data type includes built-in strings, numbers, plural numbers, and other custom classes.
Objects and instances are specific things in the class, such as men, women, and others. Here men and women can also be one type, such as older men, young man.
Remember that a class is a collective term of a type of thing, and an instance (or object) is a specific thing.
For reference only.
Example:
Class Person:
'''Basic attributes of a person: name, age, and Gender '''
Def _ init _ (self, name, age, sex ):
Self. name = name
Self. age = age
Self. sex = sex
Class Man (Person ):
Def _ init _ (self, name, age ):
Super (Man, self). _ init _ (name, age, 'male ')
Class Woman (Person ):
Def _ init _ (self, name, age ):
Super (Woman, self). _ init _ (name, age, 'female ')
What should I do if I want to learn python programming?
If you are a beginner, we recommend that you first read
Both Learning Python and dive into python have Chinese versions.
Www.python.org is the official website of python. There is a lot of information you want on it. You can check it out more often, but it is a bit depressing for people who are not good at e.
It is best to use and learn a language. Therefore, you can download and install python at www.python.org.
After you get started, "Python in a Nutshell" is a useful reference book, so that you don't have to remember too many things (such as built-in functions or modules) and can read and find them at any time.
In addition, before you write a program, google it to see if similar modules have been written on the Internet, because some cool people in foreign countries often write some useful modules, reusing them will increase your programming speed