Python instance what's going on behind a class

Source: Internet
Author: User
First look at an example, normally we define and instantiate a class as follows

Class Foo (object):   def __init__ (self):    Pass obj = Foo ()  # obj is an object instantiated through the Foo class

In the above code, obj is an object instantiated through the Foo class, in fact, not only obj is an object, but the Foo class itself is an object because everything in Python is an object.

Print type (obj) # output: Foo indicates that the Obj object was created by the Foo class
Print Type (foo) # output: type indicates that the Foo class object was created by the type class
If all things are object theory: objects are created by executing the constructor of the Foo class, then the Foo class object should also be created by executing the constructor of a class.

First, two basic classes
Here and it is necessary to mention that there are two most basic objects in Python, and that these two objects are the origin of all objects.

1, The type is (it itself), the parent class is
2, The type is , no parent class
In the Python object system, relationships are like chickens and eggs, and it is not possible to say who precedes (create), the two are interdependent, and together form the basis of the Python object system. It's hard to understand, but it doesn't matter. Tao has such two things, compared to We are not the person who design python, there is no need to make it so clear.

Ii. Creation of Classes
There are two main ways to do this, but essentially the same, all using the type class to instantiate a user class

Normal way  class Foo (object):     def func (self):  print ' Hello Wupeiqi '//Special Way (the constructor of the type Class)  def func (self ):    print ' Hello Wupeiqi '    foo = type (' foo ', (object,), {' Func ': func}) #type第一个参数: Class name #type第三个参数: Member of Class

As can be seen above, the Foo class is the type class instance, then the specific creation process is what, then look down:

To understand several concepts
new and __init () and __metaclass__:

    • The __new__ function is the function that an instance of a class will call, and whenever we invoke obj = Foo () to instantiate a class, we call __new__ () first.
    • Then call the __init__ () function to initialize the instance. __INIT__ () executed after __new__ () execution,
    • The class also has an attribute __metaclass__, which is used to indicate who created the class by whom it was instantiated, so we can view the process of class creation by setting a derived class of type class for __metaclass__.

Iii. elaboration of the operation process

1, MyType produces an instance called Foo, the main principle is set up, __metaclass__=mytypoe, so the designation MyType class to instance Foo class, if Python did not find __metaclass__, It will continue to look for the __metaclass__ property in the (parent class) and try to do the same thing as before. If Python cannot find __metaclass__ in any parent class, it will look for __metaclass__ in the module hierarchy and try to do the same. If you still can't find __metaclass__,python, you'll use the built-in type to create this class object.
2. The __new__ method in the MyType class returns an object, and all Python instances are created by this code type.__new__ (cls,name,bases,attrs)
3. MyType's __init__ () function initializes the Foo class, where we can set the Attr property of the Foo class as in the __new__ () function, such as the methods in the class, the field properties, etc.
4, as with the Foo class creation process, the Studen class inherits the Foo class, so repeat 123 steps to get a Studen class
5, when the user uses Foo () or Studen () to the instance class, the default call to the class _new_ () method, if there is no such method in the parent class to find __new__ (), we can make full use of this new function, for example, to implement the singleton pattern in Python, or batch modification of class members, and so on.
6, an instance is executed immediately after executing the __init__ () function, to initialize the instance,
7, from the above operating results can be seen, where the Foo and Studen class type is , it also proves that both the Foo class and the Studen class are made up of mytype instances ... And the third example, you can see that the type of Foo2 class is , this is no exception, Foo2 inherited the object class, (this, or skip it, has already spoken of the type and object in love to kill the relationship)
Summarize
First, you know that a class is actually an object that can create an instance of a class. Well, actually, the classes themselves are instances, of course, they are instances of the Meta class. Everything in Python is an object, either an instance of a class or an instance of a meta class, except for type. Type is actually its own meta-class, which is not something you can do in a pure Python environment, by playing some small tricks on the implementation level. Second, the meta-class is very complex. For very simple classes, you might not want to modify the class by using a meta-class. There are two other techniques you can use to modify a class:

1, Monkey Patching
2. Class Decora

The above is the detailed content of this article, I hope that everyone's study has helped.

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