The following is a special member of a class that automatically calls these special methods when we perform certain operations
1. __doc__
Represents the description of a class
>>> class Foo: "" "describes the class information, which is the Magic" "Def func (self) used to see the piece: Passprint (foo.__doc__) #输出: Description information for the class------------ -Describe the class information, which is used to see the magic of the film
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 QQ import Personjohn=person (' John ', ' Male ', 30,20) print (john.__module__) print (john.__class__)--------------QQ <class ' Qq.person ' >
3. __init__
Constructs a method that automatically triggers execution when an object is created through a class
Class Foo:def __init__ (self, name): Self.name = name Self.age = 18
obj = Foo (' AA ') # automatically executes the __init__ method in the 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. __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__-----__call__
6. __dict__
All members in a class or object
class province: country = ' China ' def __init __ (self, name, count): self.name = name self.count = count def Func (Self, *args, **kwargs): print (' func ') # Gets the members of the class, namely: Static fields, Methods, print (province.__dict__) # output: {' Country ': ' China ', ' __module__ ': ' __main__ ', ' func ': <function func at 0x10be30f50>, ' __init__ ': <function __init__ at 0x10be30ed8>, ' __doc__ ': none}obj1 = Province (' Hebei ', 10000) print (obj1.__dict__) # gets the member obj1 output of the object # : {' count ': 10000, ' name ': ' Hebei '}obj2 = province (' Henan ', 3888) print (obj2.__dict__) # get member # output for object obj1 : {' COunt ': 3888, ' name ': ' Henan '}--------------{' Country ': ' China ', ' func ': < function province.func at 0x000002286c21c620>, ' __dict__ ': <attribute ' __ dict__ ' of ' province ' objects>, ' __doc__ ': none, ' __weakref__ ': < attribute ' __weakref__ ' of ' province ' objects>, ' __module__ ': ' __main__ ', ' __init__ ': <function province.__init__ at 0x000002286c21c598>}{' count ': 10000, ' name ': ' Hebei '} {' 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 ' hhh ' obj = Foo () print (obj) hhh
8, __getitem__, __setitem__, __delitem__
Used for index operations, such as dictionaries. Each of the above means to get, set, delete data, or for list operations, which represent get, slice, delete
Dictionary operations
>>> 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 '] = ' BB ' # Automatic trigger execution __setitem__del obj[' K1 '] # automatically triggers execution __delitem____getitem__ k1__ Setitem__ k2 bb__delitem__ k1
List operations (slices)
>> class foo (object): def __getitem__ (Self, key): print (' __getitem__ ', key,type (key)) def __setitem__ (Self, key, value): print (' __setitem__ ', type (key), type (value)) def __delitem__ (Self, key): print (' __delitem__ ', key) obj = foo () result=obj[0:3]print (Result) Obj[1:3]=[2,3,4,5,6,7]del obj[2:3]----------------------__getitem_ _ slice (0, 3, none) <class ' slice ' >None__setitem__ <class ' slice ' > <class ' list ' >__delitem__ slice (2, 3, none)
9. __iter__
For iterators, lists, dictionaries, and tuples can be used for loops because the type internally defines the __iter__
>>> class Foo (object): Def __init__ (self, sq): self.sq = sq def __iter__ (self): return ITER (S ELF.SQ) obj = Foo ([11,22,33,44]) for I in Obj:print (i)------------11223344
This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1858788
Python Learning Notes-Object oriented (special members)