Magic Method

What
it is python object-oriented set of methods, from the establishment of access to the object properties, to support operators, support for special syntax of
the form
after the method name two _ before and after, such as: def __init __ (self):
Create and initialize 2.2 target
object instantiation process of
creating a class of objects: def __new __ (cls)
initialize the object: DEF the __init __ (Self)
new new method is called before init method returns to the initial object class can be rewritten but to return to the object, cls class represents the current
recycling target
def __del __ (): this method is practical and does not call for recycling, python's recovery mechanisms will automatically call this method to recover
instance
# first call the new method, used to return itself an object, then the object to the init method of
class Program (object):

def __new__(cls, *args, **kwargs):
print('call_new_method')
print(args)
return super(Program, cls).__new__(http://www.my516.com)

def __init__(self, name, age, language):
print('call_init_method')
self.name = name
self.age = age
self.language = language


the __name__ == IF '__main__':
Program = Program ( 'John', 22 is, 'Python')
Print (Program .__ dict__ magic)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
for the * args ** see kwargs meaning of the parameters * args and ** kwargs
----------------

Guess you like

Origin www.cnblogs.com/liyanyan665/p/11408547.html