0

I am trying to automate the creation of properties and don't even know where to begin tracking this down. Clearly I have a gap in my understanding of how 'prop' is treated in this example.

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

In the case of the doc, prop seems to persist

In the case of the value returned by fget, prop seems to be the last value in the loop ('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>)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149

0 Answers0