Могу ли я создать свойства класса во время __new__ или __init__?

Я хочу сделать что-то подобное, но пока не достиг большого успеха. Я хотел бы сделать каждый атрибут атрибутом, который вычисляет _lazy_eval только при обращении:

class Base(object):
    def __init__(self):
        for attr in self._myattrs:
            setattr(self, attr, property(lambda self: self._lazy_eval(attr)))

    def _lazy_eval(self, attr):
        #Do complex stuff here
        return attr


class Child(Base):
    _myattrs = ['foo', 'bar']


me = Child()
print me.foo
print me.bar

#desired output:
#"foo"
#"bar"

** ОБНОВИТЬ **

Это тоже не работает:

class Base(object):
    def __new__(cls):
        for attr in cls._myattrs:
            setattr(cls, attr, property(lambda self: self._lazy_eval(attr)))
        return object.__new__(cls)

#Actual output (it sets both .foo and .bar equal to "bar"??)
#bar
#bar

** ОБНОВЛЕНИЕ 2 **

Использовал__metaclass__ решение, но застрял вBase.__new__ вместо. Похоже, что для правильного формирования свойства необходимо более точное определение замыкания - prop ().

class Base(object):
    def __new__(cls):
        def prop(x):
            return property(lambda self: self._lazy_eval(x))
        for attr in cls._myattrs:
            setattr(cls, attr, prop(attr))
        return object.__new__(cls)

#Actual output!  It works!
#foo
#bar

Ответы на вопрос(4)

Ваш ответ на вопрос