Python object-oriented-advanced (special member of Class)

Source: Internet
Author: User

Python class, there are some members with special meanings, the details are as follows:

1.__doc__

Represents the description of a class

Class Foo: "" "    describes the classes information, which is used to watch the Magic" "    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

From LIB.AA import c  #C为自己定义的类obj = C () print obj.__module__  # output LIB.AA, i.e.: Output module print obj.__class__      # output LIB.AA . C, that is: Output class

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.

destructor method, deleting an object
Simple method of invocation: Del obj

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__ ' obj = Foo () # 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, the static fields and methods in the class belong to the class, i.e.

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 0x10be30ed 8>, ' __doc__ ': none}obj1 = Province (' Hebei ', 10000) print obj1.__dict__# get the member of Object Obj1 # output: {' count ': 10000, ' name ': ' Hebe I '}obj2 = Province (' Henan ', 3888) print obj2.__dict__# get the member of 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 ' obj = 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-*-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 '] = ' Wupeiqi '   # automatically triggers execution __setite M__del obj[' K1 ']           # automatically triggers execution __delitem__

9.__getslice__, __setslice__, __delslice__
The three methods are used for shard operations, such as: List

#!/usr/bin/env python#-*-Coding:utf-8-*-class Foo (object):    def __getslice__ (self, I, j):        print ' __getslice__ ', I,j     def __setslice__ (self, I, J, sequence):        print ' __setslice__ ', I,j     def __delslice__ (self, I, j):        print ' __delslice__ ', i,j obj = Foo () obj[-1:1]                   # Auto trigger execution __getslice__obj[0:1] = [11,22,33,44]    # Auto Trigger execution __setslice_ _del Obj[0:2]                # automatically triggers execution __delslice__

10.__iter__

For iterators, lists, dictionaries, and tuples can be used for loops because the type internally defines the __iter__

#!/usr/bin/env python#-*-coding:utf-8-*-class Foo (object):    def __init__ (self, sq):        self.sq = sq    def __ Iter__ (self):        return iter (self.sq) obj = Foo ([11,22,33,44]) for i in obj:    print I third step

11.__new__ and __metaclass__

obj = foo () # obj is an object instantiated through the Foo class
According to the theory that everything is an object: 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

Print type (obj) # output: <class ' __main__. Foo ' >     indicates that the Obj object is created by the Foo class print type (foo) # Output: <type ' type ' >              , the Foo class object is created by the type class

Class MyType (Type):    def __init__ (self, What, Bases=none, Dict=none):        super ("MyType, Self"). __init__ (What, bases , Dict)    def __call__ (self, *args, **kwargs):        obj = self.__new__ (self, *args, **kwargs)        self.__init__ (obj) Class Foo (object):    __metaclass__ = MyType    def __init__ (self, name):        self.name = name    def __new__ (CLS, *args, **kwargs):        return object.__new__ (CLS, *args, **kwargs) # First stage: Interpreter executes code from top to bottom create Foo class # Second stage: Create obj object from foo class obj = Foo ()

Python object-oriented-advanced (special member of Class)

Related Article

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.