# This is a learning note for the Liaoche teacher Python tutorial
1 , overview
The biggest difference between dynamic and static languages is the definition of functions and classes, not defined at compile time, but dynamically created at run time.
1.1 , type ()
The definition of class is created dynamically at run time, and the method of creating class is using the type () function.
The class created by the type () function is exactly the same as the direct write class, because when the Python interpreter encounters the class definition, it simply scans the syntax of the class definition and then calls the type () function to create the class.
The type () function can either return the types of an object or create a new type, for example, we can create a Hello class with the type () function:
>>> def fn (self, name= ' world '): # define function first
... print (' Hello,%s. '% name)
...
>>> Hello = type (' Hello ', (object,), Dict (HELLO=FN)) # Create Hello Class
of the type () function. 3 a parameter :
The name of the class;
inherited collection of parent classes, note that Python supports multiple inheritance, If there is only one parent, don't forget the single-element notation of a tuple ;
Class's method name and function binding, where we bind the function fn to the method name hello.
1.2 , Metaclass
in addition to using type () In addition to dynamically creating classes, you can also use Metaclass to control the creation behavior of classes.
Once you define Metaclass, you can create classes and finally create instances
Add an Add method to your custom MyList class with Metaclass:
# Metaclass is a template for a class, so you must derive from the ' type ' type:
class Listmetaclass (type): # The class name of Metaclass always ends with metaclass so that it is a clear indication that this is a metaclass
Def __new__ (CLS, name, bases, Attrs):
attrs[' Add ' = Lambda self, value:self.append (value) # An Add method was created for the class
Return type.__new__ (CLS, name, bases, Attrs)
class MyList (list, metaclass=listmetaclass): # instructs the class to be customized with Listmetaclass, passing in the keyword parameter metaclass
Pass
When we pass in the keyword parameter metaclass, the magic takes effect, it instructs the Python interpreter to create the MyList by listmetaclass.__new__ (), where we can modify the definition of the class, for example, by adding a new method, and then Returns the modified definition.
the parameters that are received by the __new__ () method are :
The object of the class that is currently ready to be created;
The name of the class;
The collection of parent classes that the class inherits;
The collection of methods for the class.
When do I need metaclass?
It is simpler to write the Add () method directly in the MyList definition. Under normal circumstances, we can't use Metaclass. However, you will always encounter the need to modify the class definition through Metaclass. ORM is a typical example.
ORM Full Name "Object Relational Mapping", that is, object-relational mapping, is to map a row of the relational database to an object, that is, a class corresponding to a table, so that the code is simpler to write, do not directly manipulate the SQL statement.
Python Learning note __7.6 using the original class