Python Learning note __7.6 using the original class

Source: Internet
Author: User
Tags class definition define function

# 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 :

    1. The name of the class;

    2. 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 ;

    3. 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 :

    1. The object of the class that is currently ready to be created;

    2. The name of the class;

    3. The collection of parent classes that the class inherits;

    4. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.