"Python" Object-oriented--special member method of class

Source: Internet
Author: User

The special member method of Class 1. __DOC__ represents the description of a class
Class Func (object): The    __doc__ method is used to print the description of the class "Def Tell"    :        pass    def enroll (self)        : Passprint (func.__doc__)    # Output: Class description Information
The results of the operation are as follows:
The __doc__ method is used to print the description information for a class

__DOC__ is the descriptive information used to print the class. Is the comment of the class.

2.__module__ and __class__

__MODULE__ represents the object of the current operation in that module

__CLASS__ represents the class of the object that is currently being manipulated

3. __init__ constructs a method that automatically triggers execution when an object is created through a class.

4.__del__

destructor, which automatically triggers execution when the object is freed in memory.

Note: This method is generally not defined because Python is a high-level language, and programmers do not need to be concerned with allocating and releasing memory because this work is done by the Python interpreter, so the destructor calls are automatically triggered by the interpreter when it is garbage collected.

5. The __call__ object is appended with parentheses to trigger execution.

Note: The execution of the construction method is triggered by the creation object, that is: Object = class name (), and the execution of the __call__ method is triggered by parentheses after the object, i.e.: Object () or Class () ()

Class Func (object):    def __init__ (self,name,age,sex):        self.name = name        Self.age = age        self.sex = Sex    def enroll (self):        print ('%s in enrolling in the school '%self.name)    # def __call__ (self):    #     Print ( "Instantiation plus Kua Good can also execute, nest grass") F = Func ("Alex", "Female") F.enroll () F ()
The results of the operation are as follows:
Alex in enrolling in the school
Traceback (most recent):
File "/home/zhuzhu/Seventh day/class method. py", line +, in <module>
F ()
TypeError: ' Func ' object is not callable

In the above code, F is an instance, and we know that the instantiated variable is not able to be executed in parentheses. Hint Error: TypeError: ' Func ' object is not callable, said not able to callable, now we add __call__ () method, as follows:

Class Func (object):    def __init__ (self,name,age,sex):        self.name = name        Self.age = age        self.sex = Sex    def enroll (self):        print ('%s in enrolling in the school '%self.name)    def __call__ (self):        print (" Instantiation Plus Kua Good can also execute, nest grass ") F = Func (" Alex "," Female ") F.enroll () F ()
The results of the operation are as follows:
Alex in enrolling in the school
instanced plus Kua Good can also be carried out, nest grass

As can be seen from the above code, the original instance with parentheses can not be executed, it is now possible to execute, because the __call__ () method, you can see that the __call__ () method, the instance on the line of the construction, so that it can execute the __call__ () method.

6. __dict__ View all members in a class or object

Class Province:    country = ' China '    def __init__ (self, Name, count):        self.name = name        Self.count = count< C4/>def func (self, *args, **kwargs):        print (' func ') # Gets the member of the class, namely: Static field, method, print (province.__dict__) # output: {' Country ': ' China ', ' __module__ ': ' __main__ ', ' func ': <function func at 0x10be30f50>, ' __init__ ': <function __init__ at 0x10b E30ed8>, ' __doc__ ': none}obj1 = Province (' Hebei ', 10000) print (obj1.__dict__) # Gets the member of the object Obj1 # output: {' count ': 10000, ' name ': ' hebei '}obj2 = Province (' Henan ', 3888) print (obj2.__dict__) # Gets the member of the object Obj1 # output: {' count ': 3888, ' name ': ' Henan '}

7.__str__ if the __str__ method is defined in a class, the return value of the method is output by default when the object is printed.

Class Foo:    def __str__ (self):        return ' alex li ' obj = Foo () print (obj) # output: Alex Li

8.__getitem__, __setitem__, __delitem__

Used for index operations, such as dictionaries. Each of the above represents the acquisition, setting, and deletion of data

Class Foo (object):    def __getitem__ (self, key):        print (' __getitem__ ', key)    def __setitem__ (self, key, value ):        print (' __setitem__ ', key, value)    def __delitem__ (self, key):        print (' __delitem__ ', key) obj = Foo () result = obj[' K1 '  # automatically triggers execution __getitem__obj[' k2 ') = ' Alex '  # automatically triggers execution __setitem__del obj[' K1 ']    

9. __new__ \ __metaclass__

Class Foo (object):    def __init__ (self, name):        self.name = NAMEF = Foo ("Alex")

In the code above, F is an object instantiated through the Foo class, in fact, not only F is an object, but the Foo class itself is an object because everything in Python is an object .

If all things are object theory: The Obj object is created by executing the constructor of the Foo class, then the Foo class object should also be created by executing the constructor of a class.

<class ' __main__. Foo ' ># 输出:<class ‘__main__.Foo‘>     表示,obj 对象由Foo类创建
<class ' type ' ># 输出:<type ‘type‘>              表示,Foo类对象由 type 类创建

So, theF object is an instance of the Foo class, and theFoo class object is an instance of the type class , that is, the Foo class object is created through the construction method of the type class.

"Python" Object-oriented--special member method of 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.