What is the difference between meta-class and inheritance?

Asked 2 years ago, Updated 2 years ago, 16 views

I'm studying Python, and the concept of meta-class came up.
Am I correct in understanding that you are changing the behavior of the method?
If so, I would appreciate it if you could tell me the difference from inheritance.

python

2022-09-30 19:18

2 Answers

Metaclass is a model of the class.
The class is an instance model.

Different class implementations change the behavior of the instance.
If you inherit a class and give it a different implementation, the instance of that inheritance class behaves as if it were the instance of the inheritance class.

Type is the standard meta-class from which the class is based.
The type determines the behavior of the class.
Typical class behavior is defined by the class implementation and allocated different memory for each instance.

If you specify a non-type meta-class, you can cause it to behave in a way that is not "common class behavior.Instances with the same memory can be generated no matter how you instantiate them, methods can be invisible even if you define them in a class, or methods and attribute values can be meaningful in the order they are defined.

  • Class inheritance is performed to modify some implementations of the inheritance source
  • Metaclass changes are made to change Python's interpretation of the class definition itself


2022-09-30 19:18

Will it replace the behavior of _new__?

If the new style class inheriting the object has an attribute called __metacclass__, the class makes a special move.During class generation, start the class set to the attribute __metacclass__ and invoke the __new__() method.

http://coreblog.org/ats/lerning-metaclass-by-reading-javascript-like-prototype-in-python/

In addition,

A class created with type.__new__() and assigned class attributes acts as the entity of the Prototype class and its subclasses.

By using __metacclass__, I believe that instances created from the same TestClass can share the same Prototype in the example above.


2022-09-30 19:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.