无法自动创建属性

时间:2018-04-07 20:55:55

标签: python python-2.7 properties

我正在尝试自动创建属性,甚至不知道从哪里开始跟踪它。很明显,我对“如何道具”的理解存在差距。在这个例子中被处理。

def makecls():
    cls = type('mycls', (object,), {})
    for prop in ['hi', 'bye']:
        def fget(self):
            return prop
        setattr(cls, prop, property(fget, doc=prop)
    return cls

在doc的情况下,prop似乎仍然存在

对于fget返回的值,prop似乎是循环中的最后一个值(' bye')。

>>> cls = makecls()
>>> y = cls()
>>> y.hi, y.bye
('bye', 'bye')                         #expecting ('hi', 'bye')
>>> cls.hi.__doc__, cls.bye.__doc__
('hi', 'bye')                          #got expected result
>>> cls.hi.fget, cls.bye.fget
(<function fget at 0x062C92F0>, <function fget at 0x062C93B0>)

0 个答案:

没有答案