objects, classes, and meta classes in Python __python

Source: Internet
Author: User
Tags unique id
Uncle Turtle invented Python, and then integrated a bunch of concepts in the language, such as: iterators, adorners, Functions, generators, classes, objects, routines, and so on.
These concepts do not seem to be a understood for beginners, but there is a more difficult concept, it is the creator of the Python world, although we rarely go directly to use it, but every day, it is today's protagonist-the Meta class.
To understand the Meta class, let's start with the object.
objects (object)Everything in Python is an object, you must have heard it (now you've 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 ': ' Zeng '}
On the right side of the equals sign is the object, the name to the object, and any object has 3 key attributes: Identity, value, type.
Identification
Identity is the same as the person ID ID, each object has a unique ID identity, throughout the life cycle will not change, you can think of identity is the object in the computer's memory address. A function ID () allows you to view the ID identification of an object.
Print ("Unique identification of the object is:" +str (ID (i)))
The unique identification of the object is: 1704834400
Print (the unique ID of the object is: +str (dicts))
The unique identification of the object is: 1918903679592
Object Value
The second property of the object is a value, and the value is well understood, for example, the value of 10,s is the value of Abc,nums is 1,2,3.
type
The object also has a very important attribute is the type, any object has its own type, the object is constructed by its type.
For example, the type of the above i is type int, the type of S is the string type, the type of the nums is the list type, the dicts type is the dictionary type, they are all constructed by the corresponding type.
You can view the type of an object by type ().
Print ("Object is of type:" +str (Kind (i)))
Print ("Object is of type:" +str (dicts))
The type of object is: <class ' int ' >
The type of object is: <class ' dict ' >

The type of the object will not change as well as the ID identity. The only thing that can change is the value. class

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

class Person (object):
    #__init__ () is a bit like a constructor, calling
    def __init__ (Self,name,gender) when instantiating a class:
    #name是实例的属性
        Self.name = name
        Self.gender = Gender
    #live是类的属性
    live = True

The person here is the custom class, class is an abstract template, neither refers to John nor Dick, and so on, now we can construct (instantiate) a concrete, real, and named object by calling this class, which is called an instance object (Instance).

Class and (instance) objects

P1 = Person ("Zhangan", "man")
print (p1.name)
P2 = person ("Lisi", "female")
print (p2.name)
Zhangan
Lisi

The P1, p2, is the instantiated (instance instance) object, the type of which is the person class, and the relationship between the class and the (instance) object is like the relationship between a vehicle mold and a real car being made. As shown below:

Print (p1) print (p1) print (
type (p1))
<__main__. Person object at 0x000002031271bbe0>
2212217600992
<class ' __main__. Person ' >

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

We just said everything is the object, the example (real car) is the object, the class (mold car) is certainly the object, because it is also a real thing.

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

Print (person) print (
ID)
print (type)
<class ' __main__. Person ' >
2446546123192
<class ' type ' >
We notice that the type of the object of person is called "type", which means that the person class was created by type, and now you have to remember that P1,P2 is an instance object, and that person is a class object, and the type of the instance object P1 is the class object person, The type of person is. Besides, what kind of ghost is this type?

Let's recap:

i=10
Print (The object is of type: +str ())
print (The object is of type: +STR (Kind (int))
The type of object is: <class ' int ' >
the type of object is: <class ' type ' >
The type of I is int,int type, and all classes are of type, which means that all classes are created by type. This type is the Metaclass, and the meta class is the class that is used to create the class, Dawson one, life two, Sansheng all things, the Meta class is the creator in Python. (The Meta class itself is also the object)

Now we all know that a class (object) can be created using the class keyword, and we know that the type of the Class (object) is kind, and now that the type is known, it can be created by type (the Meta Class).
To create a class with a meta class

As I mentioned earlier, type has a function to check the type of an object, but it has another function of dynamically creating a Class (object) as a meta class.

Person1 = Type ("Person1", (), {"Live": True})
print (Person1)

A person is a class, which is equivalent to:

Class Person1:
    live = True
print (Person1)
<class ' __main__. Person1 ' >
The syntax for creating classes with the Meta class type is:

Type (class name, parent class tuple (nullable), property dictionary)

Summary
Everything in Python is an object, a class is an object, and a class is an object, and a class is the one used to create the class.


This article refers to the public number "Zen of Python", thank you very much.
Link: Https://mp.weixin.qq.com/s/UTFQgu4q5pHFyQZBWA9qpA

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.