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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the metaclass in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the metaclass in Python".
Xiaoming, xiaohong and xiaozhang are all students. This kind of group is called Student.
Common methods for Python to define classes, using the keyword class
In [36]: class Student (object):
...: pass
If xiaoming, xiaohong, and xiaozhang are instances of the class, then:
Xiaoming = Student ()
Xiaohong = Student ()
Xiaozhang = Student ()
After creation, the _ _ class__ property of xiaoming returns the Student class
In [38]: xiaoming.__class__
Out [38]: _ _ main__.Student
The question is, the Student class has a _ _ class__ attribute, and if so, what does it return?
In [39]: xiaoming.__class__.__class__
Out [39]: type
Wow, the program is correct. Return type.
So, we might as well guess: Student class, type is type
In other words, the Student class is an object whose type is type
So, everything in Python is an object, and a class is also an object.
In Python, the class that describes the Student class is called metaclass.
According to this logical extension, the class that describes metaclass is called metaclass, and the class that describes metaclass is also called metaclass.
Smart friends will ask, since the Student class can create an instance, can the type class create an instance? If it can, the instance it creates is called: class. You guys are so smart!
By the way, the type class must be able to create instances, such as the Student class.
In [40]: Student = type ('Student', (), {})
In [41]: Student
Out [41]: _ _ main__.Student
It is identical to the Student class created with the class keyword.
The class of Python, because it is an object again, is similar to the operation of xiaoming,xiaohong objects. Support:
Assignment copy add attribute as function parameter In [43]: StudentMirror = Student # class direct assignment # class direct assignment
In [44]: Student.class_property = 'class_property' # add class attributes
In [46]: hasattr (Student, 'class_property')
Out [46]: True
Metaclass, it is true that the use of not so much, perhaps first understand these, will be able to cope with some situations. Even Tim Peters, the leader of Python:
"Metaclass is deep magic, and 99% of users should not have to worry about it at all."
Thank you for your reading, the above is the content of "what is the metaclass in Python". After the study of this article, I believe you have a deeper understanding of what the metaclass in Python is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.