How Python's meta-class is used

Source: Internet
Author: User
Tags greatest common divisor
This time for you to bring Python's meta-class how to use, the use of Python meta-class considerations are what, the following is the actual case, take a look.

Today my task is to thoroughly understand what is a meta-class and look together.

To understand the meta-class, let's start with the object.

Objects (object)

Python all objects, this sentence you must have heard (now you heard), a number is an object, a string is an object, a list is an object, a dictionary is an object, for example:

>>> i = 10>>> s = "abc" >>> nums = [1,2,3]>>> dicts = {"Name": "Zhang"}

The right side of the equals sign is the object, and the left is the name given to the object, and any object has 3 key attributes: Identity, value, type.

Identity

The identity is the same as the ID of the person's ID, each object has a unique ID identifier and will not change throughout the life cycle, and you can assume that the identity is the address of the object in the computer's memory. The ID of the object can be viewed by the function ID ().

>>> ID (i) 40592592>>> ID (s) 44980584

Object value

The second property of an object is a value that is well understood, such as the value of I is the value of 10,s is the value of Abc,nums is a number of three.

Type

Object also has a very important property is the type, any object has its own type, the object is constructed by its type, such as the above i is the type int, this object is constructed by int. The S type is a string type, the type of nums is a list type, and the type of dicts is the dictionary type, which is constructed from the corresponding type.

You can view the type of the object through type ().

>>> type (i) <class ' int ' >>>> type (s) <class ' str ' >>>> type (nums) <class ' List ' >>>> type (dicts) <class ' dict ' >

The type of the object is also the same as the ID identifier, and it will no longer change after it is determined.

Classes and (instance) objects

In addition to the types of integers, string types, lists, etc. that the system has already defined, we can create our own types, defined by the keyword class. For example:

>>> class Person:  # Here's self refers to an instance object itself ...  Def init (self, name):   # name is the attribute   of the instance ... Self.name = Name  # Live is the property of the class  live = True

The person here is a custom class, the class is an abstract template, neither Zhang San nor John Doe, now we can call this class to construct (instantiate) a concrete, real, named object, which is called an instance object.

>>> P1 = person ("Zhangsan") >>> p1.name ' Zhangsan ' >>>>>> p2 = person ("Lisi") > >> p2.name ' Lisi '

The P1 and P2 here are the instantiated (instance) objects, both of which are of the person class, and the relationship between the class and the (instance) object is like a vehicle mold in relation to a built-in real car.

>>> P1<main. The person object is at 0x0195aa30>>>> type (p1) <class ' main. Person ' > # Here's main is the module name

Class is also an object (also called a class object)

We just said that everything is the object, the example (real car) is the object, the class (mold) Of course is also the object, because it is also a real existence of things,

When the Python interpreter executes this instruction to the keyword class, a class called "person" is created internally, and this class is also an object, which we call a class object (note the difference between instance objects), which has the same ID identifier, type, and value. For example:

>>> ID (person) 26564024>>> type (person) <class ' type ' >>>> person<class ' main. Person ' >

We notice that this person of this class object is called "type", that is, the person class is created by type, and now you have to remember that P1,P2 is an instance object, and the person is the class object. Besides, what kind of ghost is this type?

Let's review that the type of the instance object P1 is the type of the class object Person,person.

>>> nums = [1,2,3]>>> type (nums) <class ' list ' >>>> type (list) <class ' type ' >

The type of nums is the type of list,list, the type of the Dictionary class (Dict), and the type of all classes, which means that all classes are created by type. This type is the Meta class, Daosh one, life two, Sansheng all things, the meta-class is the creator of Python. (the meta-class itself is also an object)

Now we all know that a class (object) can be created using the class keyword, and we also know that the class (object) is of type, and since the type is known, it can certainly be created by type (the Meta Class).

To create a class with a meta class

As mentioned earlier, type has a role to check the type of the object, in fact it has another function is to create a class (object) dynamically as a meta-class.

>>> person = type ("Person", (), {"Live": True}) >>> Person<class ' main. Person ' >

A person is a class, which is equivalent to:

>>> class Person: ...  Live = True...>>> Person<class ' main. Person ' >

The syntax for creating a class with the meta-class type is:

Type (class name, base class tuple (can be empty), property dictionary)

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Python implementation method of solving greatest common divisor

How numpy and arrays should be converted in Python

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.