special members of the class
1. __doc__
represents the description of a class
Class Foo: "" describes the classes information, which is used to see the Magic "" " def func (self): passprint foo.__doc__============== describes the class information, This is the magic for watching the film
2. __module__ and __class__
__module__ indicates which module the object of the current operation is in
__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 from a class.
Class Foo: def__init__ (Self, name): self.name = name self.age = 18obj = Foo (' Wupeiqi ') # Auto-Execute __init__ method in 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.
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): passdef__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, and the static fields and methods in the class belong to the class, namely:
Class Province: country = ' China ' def__init__ (self, Name, count): self.name = name Self.count = count< C5/>def func (self, *args, **kwargs): print ' func ' # Gets the members of the class, i.e.: Static fields, Methods ...
Print province.__dict__# output: {' Country ': ' China ', ' __module__ ': ' __main__ ', ' func ': <function func at 0x10be30f50> ' __init__ ': <function __init__ at 0x10be30ed8>, ' __doc__ ': none}# gets the member of the object obj1
obj1 = Province (' Hebei ', 10000) print obj1.__dict__# output: {' count ': 10000, ' name ': ' Hebei '}
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
slice :
Slice Cutting
#!/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): C16/>print ' __delslice__ ', i,jobj = Foo () obj[-1:1] # Auto trigger execution __getslice__obj[0:1] = [11,22,33,44] # automatic trigger execution __ Setslice__del Obj[0:2] # automatically triggers execution __delslice__
Ten. __iter__
for iterators, lists, dictionaries, and tuples can be used for loops because the type internally defines the __iter__
The
first step:
Class Foo (object): passobj = foo () for i in obj: print i # error: TypeError: ' Foo ' object is not iterable can Iteration of
Step Two:
#!/usr/bin/env python#-*-coding:utf-8-*-class Foo (object): def__iter__ (self): passobj = Foo () for I in obj:< C11/>print I# error: Typeerror:iter () returned non-iterator of type ' Nonetype '
Step Three:
#!/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
As you can see from the above steps, the for loop iteration is actually ITER ([11,22,33,44]), so the execution process can be changed to:
#!/usr/bin/env python#-*-coding:utf-8-*-obj = iter ([11,22,33,44]) for i in obj: print I forloop syntax internal #!/usr /bin/env python#-*-Coding:utf-8-*-obj = iter ([11,22,33,44]) while True: val = obj.next () print Val
__new__ and __metaclass__ .
Read the following code:
Class Foo (object): def __init__ (self): Pass obj = Foo () # obj is an object instantiated through the Foo class
in the above code, obj is an object instantiated through the Foo class, in fact, not only obj 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.
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
Therefore, theobj 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 constructor of the type class.
Then there are two ways to create a class:
a). Normal mode
Class Foo (object): def func (self): print ' Hello Wupeiqi '
b).特殊方式(type类的构造函数)
def func: print ' Hello Wupeiqi ' foo = type (' foo ', (object,), {' Func ': func}) #type第一个参数: Class name #type second argument: base class for the current class # Type third parameter: A member of a class
==》 类 是由 type 类实例化产生
python-Object-Oriented (iii)--Special members of classes