Get the App
SLTechnology News&Howtos  ›  Development  › 

How python creates metaclass

Shulou Source: shulou.com Published: 2022-06-03 17:39:05 11月14日 Update

This article mainly introduces how to create metaclass in python. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

Each time a class is created, the default Metaclass type is called. The metaclass contains information such as the name, base class set, and attributes associated with this class. Therefore, when you instantiate a class, the class with these parameters is called. Metaclasses can be created in two ways: type classes and custom metaclasses.

1. Python uses the Type class behind the scenes to create all classes, with a built-in metaclass called type.

Unlike Java or C, there are major data types. Every variable or object in Python has a class associated with it.

Class Edureka (): obj = Edureka () print (type (obj)

2. Custom metaclass modifies the work of the class by performing actions or code injection. You can pass Metaclass as a keyword when creating a class definition.

In addition, we can do this by simply inheriting classes instantiated by this Metaclass keyword.

Class EduFirst (type): def _ _ new__ (cls, name, base_cls, dict): passclass EduSecond (type): def _ _ init__ (self, name, base_cls, dict): pass is all the content of the article "how python creates metaclasses". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Type key keyword content can be passed example article different code value information interest action parameter variable name object boy partner attribute Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux OPPO Reno Shulou Tech Info Xiaomi