Class Learning notes in Python

Source: Internet
Author: User
This article mainly introduces the class learning notes in Python. This article is a personal learning summary. it uses examples to explain the class definition and access control, if you need it, you can refer to the object-oriented language in Python to support inheritance and polymorphism;

Define a Person class:

The code is as follows:


>>> Class Person:
... Def sayHello (self ):
... Print ('Hello ')
...
>>> Person. sayHello (None)
Hello
>>> Person (). sayHello ()
Hello


Class methods for modifying Person

The 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;

The 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;

The code is as follows:


>>> Class Person:
... Name = 'unkown'
... Def sayHello (self ):
... Print ('I \ 'M' + name)
...
>>>
>>> Person. sayHello (None)
Traceback (most recent call last ):
File" ", Line 1, in
File" ", Line 4, in sayHello
NameError: name 'name' is not defined
>>> P = Person ()
>>> P. name = 'wyj'
>>> P. sayHello ()
Traceback (most recent call last ):
File" ", Line 1, in
File" ", 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;

The 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" ", Line 1, in
File" ", 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?

The 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 );

The 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" ", Line 1, in
AttributeError: 'Person 'object has no attribute '_ private_method'


In fact, all the above underlined methods have a _ ClassName _ methodName method.

The code is as follows:


>>> Person. _ Person _ private_method


Call

The 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.

The 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;

The 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.