Código: Metaclasses são o tipo das classes

This commit is contained in:
Luiz Otávio
2022-11-29 10:23:00 -03:00
parent 1d916bf9c0
commit 8869d5ba5a

View File

@@ -22,14 +22,47 @@
# com certeza que precisam delas e não precisam de uma explicação
# sobre o porquê)."
# — Tim Peters (CPython Core Developer)
# object acima
# class Foo:
# ...
def meu_repr(self):
return f'{type(self).__name__}({self.__dict__})'
Foo = type('Foo', (object,), {})
f = Foo()
# print(isinstance(f, Foo))
print(type(f))
print(type(Foo))
class Meta(type):
def __new__(mcs, name, bases, dct):
print('METACLASS NEW')
cls = super().__new__(mcs, name, bases, dct)
cls.attr = 1234
cls.__repr__ = meu_repr
if 'falar' not in cls.__dict__ or \
not callable(cls.__dict__['falar']):
raise NotImplementedError('Implemente falar')
return cls
def __call__(cls, *args, **kwargs):
instancia = super().__call__(*args, **kwargs)
if 'nome' not in instancia.__dict__:
raise NotImplementedError('Crie o attr nome')
return instancia
class Pessoa(metaclass=Meta):
# falar = 123
def __new__(cls, *args, **kwargs):
print('MEU NEW')
instancia = super().__new__(cls)
return instancia
def __init__(self, nome):
print('MEU INIT')
# self.nome = nome
def falar(self):
print('FALANDO...')
p1 = Pessoa('Luiz')
p1.falar()