Python object-oriented grooming--------3. Object-oriented advanced--special members of class

Source: Internet
Author: User

The class members of Python and the member modifiers are described above to understand that there are three categories of fields, methods, and properties in a class, and that if there are two underscores before the member name, the member is a private member and the private member can only be called from within the class. No matter how people or things tend to be out of the way, Python's class members are the same, there are some special meaning members, the details are as follows:

1. __doc__

Represents the description of a class

class Foo:     """  "" "    def  func (self):        passprint Foo.  __doc__# Output: Class description Information

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

# !/usr/bin/env python # -*-coding:utf-8-*- class C:     def __init__ (self):         ' Wupeiqi ' Lib/aa.py
 from Import  = C ()print obj.__module__  #  output LIB.AA, i.e.: Output module Print obj.__class__      #  output lib.aa.c, i.e.: Output class

3. __init__

Constructs a method that automatically triggers execution when an object is created from a class.

class Foo:     def __init__ (self, name):         = name        = *= Foo ('wupeiqi'#  __init__ in class automatic execution Method

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.

class Foo:     def __del__ (self):         Pass

5. __call__

The 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 Foo:     def __init__ (self):         Pass        def __call__ (Self, *args, * *Kwargs)        : Print ' __call__ '  # execute __init__obj ()       #  Execute __call__

6. __dict__

All members in a class or object

As we know above, the ordinary fields of the class belong to the object, and the static fields and methods in the class belong to the class, namely:

classProvince:country=' China'    def __init__(self, Name, count): Self.name=name Self.count=CountdefFunc (self, *args, * *Kwargs):Print 'func'#gets the members of the class, that is: static fields, methods,PrintProvince.__dict__#output: {' Country ': ' China ', ' __module__ ': ' __main__ ', ' func ': <function func at 0x10be30f50> ' __init__ ': <func tion __init__ at 0x10be30ed8>, ' __doc__ ': None}obj1= Province ('Hebei', 10000)PrintObj1.__dict__#gets the member of the object Obj1#output: {' count ': 10000, ' name ': ' Hebei '}Obj2= Province ('Henan', 3888)PrintObj2.__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 ' Wupeiqi '  = Foo ()print  obj#  output: Wupeiqi

8, __getitem__, __setitem__, __delitem__

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

#!/usr/bin/env python#-*-coding:utf-8-*- classFoo (object):def __getitem__(self, key):Print '__getitem__', Keydef __setitem__(self, Key, value):Print '__setitem__', Key,valuedef __delitem__(self, key):Print '__delitem__', key obj=Foo () result= obj['K1']#automatic trigger Execution __getitem__obj['K2'] ='Wupeiqi'   #automatic trigger Execution __setitem__delobj['K1']#automatic trigger Execution __delitem__

Python object-oriented grooming--------3. Object-oriented advanced-special members 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.