Метакласс Python: создайте атрибуты, например, из атрибутов метакласса.Python

Программы на Python
Гость
Метакласс Python: создайте атрибуты, например, из атрибутов метакласса.

Сообщение Гость »

Код: Выделить всё

class Meta(type):
def __new__(cls, name, bases, attrs):
# Generate a and b for the object based on x ?
return super().__new__(cls, name, bases, attrs)

class A(metaclass=Meta):
def __init__(self, a, b):
self.a = a
self.b = b

obj = A(x)
The passed to should be consumed with in the meta class , and it should generate the attributes & that are needed by the

Код: Выделить всё

__init__
of . It is preferred for not to have access to .
Not sure if its possible or valid, but is there a way to achieve it ?


Источник: https://stackoverflow.com/questions/781 ... r-metaclas

Вернуться в «Python»