Class learning notes in Python _python

Source: Internet
Author: User

Python uses object-oriented language to support inheritance and polymorphism;

Define a person class:

Copy Code code as follows:

>>> class Person:
... def sayhello (self):
... print (' Hello ')
...
>>> Person.sayhello (None)
Hello
>>> person (). SayHello ()
Hello

class method that can modify person
Copy Code code 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 that can be assigned to variables and can be called directly;
Copy Code code as follows:

>>> Person.sayhello is person (). SayHello
False
>>> Person.sayhello = = Person (). SayHello
False

Person.sayhello and Person (). SayHello is not the same object, intuitively, person (). SayHello Association (binding) A person instance, and Person.sayhello is a class method;

The self argument is, in fact, the difference between methods and functions: Methods bind their first argument to the owning instance, so this parameter is not required;
Copy Code code 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

Visible, Python in the resolution of variables, the default from the local scope/global scope to find;
Copy Code code 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

The access member is passed self, and if Person.sayhello (obj) is invoked with an object that contains the name attribute, is it OK?
Copy Code code as follows:

>>> class Cat:
... name = ' Huanhuan '
...
>>> Person.sayhello (Cat ())
I ' M Huanhuan

OK, Python does not limit the need to invoke class methods with similar instance objects as arguments (seemingly Python's class mechanism resembles JavaScript);

Access control

Python does not directly support private access, but depends on the programmer's own grasp.

However, you can have a double underline before the property name to give it private access (invisible);

Copy Code code 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, a method that starts with an underscore has a _classname__methodname method.
Copy Code code as follows:

>>> Person._person__private_method
<function Person.__private_method at 0x7fed431a2d90>

Call
Copy Code code as follows:

>>> Person._person__private_method (None)
Private

In short, Python does not prevent method calls from outside the class;

Class properties and Object properties

First, you can add a property to the class, and the new object will get a copy of the property

Copy Code code as follows:

>>> Person.age = 3
>>> person ().
3
>>> Person.age = 4
>>> person ().
4
>>> p = person ()
>>> Person.age = 31
>>> P.age
31

The modification of the class attribute is reflected on the properties of the previously generated object, which indicates that the properties of the class attribute and the object share a value;

Copy Code code as follows:

>>> P.age = 34
>>> P.age
34
>>> Person.age
31
>>> person.age = 99
>>> P.age
34

Once the property of the object is modified, the object property has its own value, and it is not reflected on the class attribute, and the modification of the class attribute is no longer reflected on the object's attribute;

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.