Python meta-class programming

Source: Internet
Author: User

What is a meta-class? Young people do not care about these details, we step-by-step!

001.

In the world of OOP, there is a word, "all things are objects."

classPerson (object): Name=Noneif __name__=="__main__": I=123s="Hello World"P=Person ()Print(Type (i))#<class ' int ' >    Print(Type (s))#<class ' str ' >    Print(Type (p))#<class ' __main__. Person ' >    Print(Type (int))#<class ' type ' >    Print(Type (str))#<class ' type ' >    Print(Type (person))#<class ' type ' >

Let's take a look at the meaning of the above code

1, the first three sentences we can see: I is an instance of the int class, S is an instance of the Str class, p is an instance of the person class; #我下面要说的话 can make you feel uncomfortable

2, the last three sentences we can see:Int,str,person These classes are actually just an instance of the type class !

002.

I really did not tease you, type it really is a class Ah! Don't believe you help (type) look at

classtype (object)|type (object_or_name, bases, Dict)| Type (object), the object'S Type| Type (name, bases, Dict)a new type| |Methods defined here:| |__call__(Self,/, *args, * *Kwargs)|Call self as a function.| |__delattr__(Self, name,/) |Implement delattr (self, name).| |__dir__(...) |__dir__()List| Specialized__dir__Implementation forTypes| |__getattribute__(Self, name,/) |Return getattr (self, name).| |__init__(Self,/, *args, * *Kwargs)|  Initialize self. See Help (Type) foraccurate signature.| |__instancecheck__(...) |__instancecheck__()BOOL| CheckifAn object isAn instance| |__new__(*args, * *Kwargs)| Create and returnA new object. See Help (Type) forAccurate signature.

----

Python meta-class programming

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.