When you create and instantiate a class in Python, a lot happens and I think that not everyone is familiar with the steps. Let's look at the sequence. Class creation When you create bedside lamps a class with the statement class, the first thing that happens is the builder (__new__) the metaclass (for standard type) is called. The constructor takes as parameters the metaclass itself, the class name (str or bytes), a tuple containing the base classes of the class and created a dictionary of methods and attributes declared in the class body. If you override the constructor of the metaclass, it needs to return the instantiated object, which you get from the parent class constructor of the metaclass. Then the boot method (__init__) the metaclass is called, receiving the return parameters as the constructor, the class name, the tuple of base classes and the dictionary of attributes and methods. It is recommended not to overwrite the constructor of the metaclass. Any procedure can be safely performed in the methods and initialization and call. Instantiation of the class when you instantiate the class, the first method to be called is the calling method (__call__) the metaclass. It receives as parameters the class and any passed in command of instantiation bedside lamps parameters. Then it evoked bedside lamps the class constructor, and the class receiving any command passed on instantiation parameters. It is mandatory that the instance created by the constructor of super is returned if the constructor bedside lamps is overwritten. After the constructor, the initialization method of the class is called, receiving the return parameters as the constructor and any passed in command bedside lamps of instantiation parameters. If calling the instance method call is implemented in the class, the instance bedside lamps is "callable" (callable). On calling the method call runs getting bedside lamps the instance and any parameters passed in the call instance. Who's Who A small code only boilerplates not running anything, for the sole purpose bedside lamps of showing where it is each method cited: class metaclass (type): def __new __ (meta, name, base, dict_): "" "This is the Builder metaclass "" "return type .__ new __ (meta, name, base, dict_) __init __ (cls, name, base, dict_):" "" This is the initialization method of the metaclass "" "type .__ init __ (cls , name, base, dict_) def __ __call (cls, * args, ** kwargs): "" "This method is called the metaclass" bedside lamps "" return type .__ __ call (cls, * args, ** kwargs) class Class (object): __metaclass__ = metaclass def __new __ (cls, * args, ** kwargs): "" "This is the class constructor" "" super return (Class cls) .__ new __ (cls) __init __ (self, * args, ** kwargs): "". "This is the initialization method of the class As she inherits from object, no need to call super, but when the form is: super (Class, self) .__ __ init (* args, ** kwargs) "" bedside lamps "def __ __call (self, * args, ** kwargs):" "" This method is called the class "" "return None
A chat about destructor The destructor method method (__del__) is not called when the object loses all active references, but when it is collected by the garbage collector (GC). As the gc has no right time to run and their behavior varies greatly bedside lamps from an implementation of Python to another, it is not recommended to rely on it to perform critical procedures. What you can do is use it to ensure that certain states may have been forgotten or lost after the instance was without references. Concluding remarks The signatures of the builder and the class initialization method must be strictly equal. [Update] One important detail is that if the initialization method is overwritten by changing your signature, no need to override the constructor, but the constructor is overridden by changing your signature, it is mandatory that the initialization method is also overridden. Details of language ... [/ update] The method call call the parent class of the target class must pass the arguments expected by the signature of the constructor and the class initialization parameters. bedside lamps In the above example, this call (preceded bedside lamps by return) is: type .__ __ call (cls, * args, ** kwargs) [update] A detail I forgot to mention bedside lamps is that this is precisely called the builder and the initialization method are evoked. [/ Update]
When you create and instantiate a class in Python, a lot happens and I think that not everyone is familiar with the steps. Let's look at the sequence. Class creation When you create a class with the statement class, the first thing that happens is the builder bedside lamps (__new__) the metaclass (for standard type) is called. The constructor takes as parameters the metaclass itself, the class name (str or bytes), a tuple containing the base classes of the class and created a dictionary of methods and attributes declared in the class body. If you are
No comments:
Post a Comment