python--class

Source: Internet
Author: User

The definition format of the class:

Class CLASSNAME (parent Class 1, parent Class 2, parent Class 3):

__a = 0 # #私有属性, two _ starts, only functions in that class can access

B = 0 # #公共属性

def function name (self):

Self.__a = ... #访问私有属性名

Self. B = ... #访问公共有属性名

Ps:

1, when defining the function, must have self this parameter, and must be placed in the first position

2, when defining private functions, function names and private properties, the same as the two _ start

3, the inherited parent class can be multiple, more than one parent class has the same function, if other functions to call this function, then the search from left to right, will only access the first parent class with the function

4. You can override a function in a parent class in a subclass

5, if you want to instantiate or inherit the class of other modules, you must first import the module with import, and then instantiate or inherit

6. The proprietary methods in the class are as follows:

1 __init__ Constructor that is called when the object is generated.
2 __del__ destructor, which is used when releasing objects
3 __setitem__ Assigning values by index
4 __repr__ Print, convert
5 __getitem__ Get values by index
6 __len__ Get length
7 __cmp__ Comparison operation
8 __add__ Add operation
9 __sub__ Minus operations
10 __mull__ Multiply operation
11 __div__ Except operations
12 __mod__ To find the remainder operation
13 __pow__ exponentiation operation
14 __call__ Function call

python--class

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.