Python Class Summary

Source: Internet
Author: User

Python can inherit multiple parent classes, multiple inheritance.

Class supports the generation of multiple objects, inheritance of namespaces, operator overloading
1), class produces multiple instance objects
Two types of objects in the Python OOP model: Class objects and instance objects.
The class object provides the default behavior, which is the factory of the instance object. Instance objects are the actual objects that the program handles: each has a separate namespace. The class object comes as far as the statement, while the instance comes to the call. Each time a class is called, a new instance of the class is obtained.
A, class object provides default behavior
b, the instance object is the concrete element
2), class through inheritance to customize
A, class through inheritance to customize
B, class is a property inside a module
3), class can intercept Python operators

Common operator overloading methods
Method overload Call
__init__ constructor Method Object establishment: X=class ()
__del__ destructor Method Object retraction
__add__ operator + x+y,x+=y
__sub__ operator-X-y,x-=y
__OR__ operator |    (bit or) X| Y x|=y
__repr__,__str__ printing, converting print X "__str__", repr (x), str (x)
__call__ function call X ()
__getattr__ Point number Operation x.undefined
__setattr__ Property Assignment Statement X.any=value
__GETITEM__ index Operation X[key], for loops and other iterators without __iter__
__SETITEM__ Index Assignment Statement X[key]=value
__len__ length Len (X), Truth test
__cmp__ comparison X==y,x
__lt__ Specific comparison x<y (or else __cmp__)
__eq__ Specific comparison x==y (or else __cmp__)
__radd__ Left add + noninstance + X
__iadd__ field (Enhanced) addition x+=y (or else __add__)
__ITER__ iterative environment for loops, tests, lists, mappings, and more
The names of all overloaded methods have two underscore characters before and after them, so that the variable names defined in the same class are distinguished from each other. The mapping of a particular method name to an expression or an operation is predefined by the Python language.

Python Class Summary

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.