我在玩弄巨蟒来理解它是如何工作的,但是有一些奇怪的东西。我在定义
__new__
方法在我的元类中,我希望第四个参数(它是类属性的dict)包含在我的类中定义的方法为body。
我有这个代码:
#!/usr/bin/python3
class MyMetaClass(type):
def __new__(metacls, name, bases, attrs):
instance = super(MyMetaClass, metacls) #Create instance of super class, which metaclass is type
print(type(type))
print(type(type(instance)))
print(instance.__new__)
print(type.__new__)
print(attrs)
print(getattr(attrs, 'foobar'))
return super(MyMetaClass, metacls).__new__(metacls, name, bases, attrs)
class TestClass(metaclass=MyMetaClass):
def __get__(self, obj, type=None):
return self
def foobar(self):
print(f'My name is {self.name}')
def __init__(self, args):
print(args)
self.firstname = args['firstname'] if 'firstname' in args else ''
self.lastname = args['lastname'] if 'lastname' in args else ''
self.name = f'{self.firstname} {self.lastname}'
data = {'firstname': 'Florian'}
c = TestClass(data)
c.foobar()
输出为:
<class 'type'>
<class 'type'>
<built-in method __new__ of type object at 0x10d759e40>
<built-in method __new__ of type object at 0x10d759e40>
{'__module__': '__main__', '__qualname__': 'TestClass', '__get__': <function TestClass.__get__ at 0x10e192b70>, 'foobar': <function TestClass.foobar at 0x10e192bf8>, '__init__': <function TestClass.__init__ at 0x10e192c80>}
Traceback (most recent call last):
File "<stdin>", line 25, in <module>
File "<string>", line 13, in <module>
File "<string>", line 10, in __new__
AttributeError: 'dict' object has no attribute 'foobar'
所以
print(attrs)
在里面
MyMetaClass.__new__
结果如预期的那样:
{'__module__': '__main__', '__qualname__': 'TestClass', '__get__': <function TestClass.__get__ at 0x7f042b9e6158>, 'foobar': <function TestClass.foobar at 0x7f042b9e61e0>, '__init__': <function TestClass.__init__ at 0x7f042b9e6268>}
你怎么看,它包含“foobar”键。
然而,下一行上升
AttributeError : 'dict' object has no attribute 'foobar'
.为什么?
我试过了
hasattr(attrs, 'foobar')
也会返回假。