In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to understand the metaclass of Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is metaclass?
In Python, everything is an object, the numbers, strings, functions, lists, etc. we define are all objects, objects are instances of class, and classes are actually objects, instances of type. This type is the metaclass in Python. Metaclass is used to create all types of classes, and all new classes in Python and all classes in Python3 are instances of type metaclass. Let's look at the following example:
Print (type (0)) # print (type (int)) # print (type ("tigeriaf")) # print (type (str)) # print (type ([1,2,3])) # print (type (list)) # class User: passu = User () print (type (u)) # print (type (User) # print (type (type)) # type metaclass dynamic creation class
Before we defined the class to use the class keyword to create, in addition to this we can also use type to create the class dynamically.
The usage is as follows:
Type (name, bases, dict), which receives three parameters
The first parameter, name, is the name of the class to be created.
The second parameter, bases, refers to the tuple that needs to inherit the parent class
The third parameter, dict, is the property of the class.
For example:
Class User: def _ init__ (self): self.name = 'tigeriaf'print (User) user = User () print (user.name) class User: def _ init__ (self): self.name =' tigeriaf'print (User) user = User () print (user.name)
Both of the above methods can create classes, and the output is the same, so it is very convenient to use type to create classes dynamically.
Custom metaclass
From the above example we know that we can use type (name, bases, dict) to create a class, if using type metaclass can not meet some of our needs, can we customize a metaclass and use it to create a class? The answer is yes, let's take a look at it:
Class MyMetaClass (type): def _ init__ (cls, name, bases, dict): super (). _ _ init__ (name, bases, dict) cls.int_attrs = {} for k, v in dict.items (): if type (v) is int: cls.int_ attrs [k] = vUser = MyMetaClass ('User', (), {' name': 'tigeriaf', "age": 24 "level": 2, "introduction": "Python engineer"}) print (User) # user = User () print (user.name) # tigeriafprint (user.int_attrs) # {'age': 24,' level': 2}
You can also use the following method to create a class that inherits a metaclass.
Class User (metaclass=MyMetaClass): pass
Note: using a metaclass in Python2 requires assigning a value to _ _ metaclass__ within the class to be created, and the value is metaclass.
The above code defines a class MyMetaClass, inherited from the type class, because type is a metaclass, so MyMetaClass is also a metaclass, in _ _ init__ through super (). _ _ init__ (name, bases, dict) called the parent class type _ _ init__ () method, in the implementation of the custom metaclass, but also in the creation of the class when the property loop, and then the value of the int type of the attribute stored separately. In this way, we have implemented a metaclass that is more customized than the type metaclass, and we are free to add the functionality we want in the metaclass.
This is the end of the content of how to understand the metaclass of Python. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.