Python元类的__new__方法中的奇怪行为

时间:2015-04-10 12:04:51

标签: python oop behavior metaclass

我在Python元类的__new__方法中遇到了奇怪的行为。我知道以下代码可以正常工作:

def create_property(name, _type):

    def getter(self):
        return self.__dict__.get(name)

    def setter(self, val):
        if isinstance(val, _type):
            self.__dict__[name] = val
        else:
            raise ValueError("Type not correct.")

    return property(getter, setter)


class Meta(type):

    def __new__(cls, clsname, bases, clsdict):
        for key, val in clsdict.items():

            if isinstance(val, type):
                clsdict[key] = create_property(key, val)

        return super().__new__(cls, clsname, bases, clsdict)

但是,当避免定义define_property函数并将代码置于for __new__内时,会发生奇怪的事情。以下是修改后的代码:

class Meta(type):

    def __new__(meta, name, bases, clsdict):

        for attr, data_type in clsdict.items():
            if not attr.startswith("_"):

                def getter(self):
                    return self.__dict__[attr]

                def setter(self, val):
                    if isinstance(val, data_type):
                        self.__dict__[attr] = val
                    else:
                        raise ValueError(
                            "Attribute '" + attr + "' must be " + str(data_type) + ".")

                clsdict[attr] = property(getter, setter)

        return super().__new__(meta, name, bases, clsdict)

这个想法是能够创建表现形式的类,即:

class Company(metaclass=Meta):
    name = str
    stock_value = float
    employees = list

if __name__ == '__main__':

    c = Company()
    c.name = 'Apple'
    c.stock_value = 125.78
    c.employees = ['Tim Cook', 'Kevin Lynch']

    print(c.name, c.stock_value, c.employees, sep=', ')

执行时,会发生不同的错误,例如:

Traceback (most recent call last):
  File "main.py", line 37, in <module>
    c.name = 'Apple'
  File "main.py", line 13, in setter
    if isinstance(val, data_type):
TypeError: isinstance() arg 2 must be a type or tuple of types

Traceback (most recent call last):
  File "main.py", line 38, in <module>
    c.stock_value = 125.78
  File "main.py", line 17, in setter
    "Attribute '" + attr + "' must be " + str(data_type) + ".")
ValueError: Attribute 'name' must be <class 'str'>.

Traceback (most recent call last):
  File "main.py", line 37, in <module>
    c.name = 'Apple'
  File "main.py", line 17, in setter
    "Attribute '" + attr + "' must be " + str(data_type) + ".")
ValueError: Attribute 'stock_value' must be <class 'float'>.

Traceback (most recent call last):
  File "main.py", line 37, in <module>
    c.name = 'Apple'
  File "main.py", line 17, in setter
    "Attribute '" + attr + "' must be " + str(data_type) + ".")
ValueError: Attribute 'employees' must be <class 'list'>.

那么,这里发生了什么?单独定义create_property__new__方法之间有什么区别?

1 个答案:

答案 0 :(得分:1)

这是由于作用域和变量绑定在python中的工作原理。您可以在循环中定义一个访问局部变量的函数;但是在函数的 execution 期间查找了这个局部变量,在定义期间没有绑定:

fcts = []
for x in range(10):
    def f(): print x
    fcts.append(f)
for f in fcts: f() #prints '9' 10 times, as x is 9 after the loop

正如您所发现的,您可以使用效用函数简单地在当前循环值上创建一个闭包:

fcts = []
def make_f(x):
    def f(): print x
    return f

for x in range(10):
    fcts.append(make_f(x))
for f in fcts: f() #prints '0' to '9'

另一种可能性是(ab)使用默认参数,因为它们是在函数创建期间分配的:

fcts = []
for x in range(10):
    def f(n=x): print n
    fcts.append(f)
for f in fcts: f() #prints '0' to '9'