Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python uses metaclass type to create classes

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how python uses metaclass type to create classes". In daily operation, I believe many people have doubts about how python uses metaclass type to create classes. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about how python uses metaclass type to create classes. Next, please follow the editor to study!

Catalogue

1. Type dynamically creates classes

1.1 grammatical format

1.2 case 1: creating classes using type

Case 2: using type to create a class with properties (methods)

Case 3: use type to dynamically create a class that inherits the specified class

Foreword:

Usually we use the class class name to create a class, but have you ever wondered who created the class? it is often said in python that everything is an object, an object is created by a class, and that class itself can be regarded as an object, and a class can be created by a metaclass type.

1. Type dynamically creates class 1.1 syntax format

Type (class name, tuple of parent class name (which can be empty), dictionary containing attributes (name and value)

Case 1: use type to create class Person = type ("Person", (), {}) p = Person () print (type (p)) print (Person.__name__)

Results:

Person

Note: the Person in type ("Person", (), {}) can be written as any other string, but when you print the name of the class, it becomes the other string you write.

Person = type ("Per", (), {}) p = Person () print (Person.__name__)

Results:

Per

Therefore, in order to make the program code more friendly, the general variable name and the set class name remain the same.

Case 2: use type to create a class def show (self) with attributes (methods): print ("show yourself") Person = type ("Person", (), {"age": 18, "name": "jkc", "show": show}) p = Person () print (p.age) print (p.name) p.show ()

Results:

eighteen

Jkc

Show yourself.

We dynamically create a class with a parent class of Object, attributes of age and name, and method of show

Case 3: use type to dynamically create a class class Animal that inherits the specified class: def _ init__ (self, color= "blue"): self.color = color def eat (self): print ("eat") Dog = type ("Dog", (Animal,), {}) dog = Dog () dog.eat () print (dog.color)

Results:

Eat

Blue

We have dynamically created a Dog class that inherits the Animal class, which can use all the methods and properties of the Animal class

At this point, the study on "how python uses metaclass type to create classes" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report