目标是创建一个行为类似db结果集的模拟类。
例如,如果一个数据库查询返回,使用dict表达式,{'ab':100, 'cd':200},那么我想看到:
>>> dummy.ab
100
一开始我想也许我可以这样做:
ks = ['ab', 'cd']
vs = [12, 34]
class C(dict):
def __init__(self, ks, vs):
for i, k in enumerate(ks):
self[k] = vs[i]
setattr(self, k, property(lambda x: vs[i], self.fn_readyonly))
def fn_readonly(self, v)
raise "It is ready only"
if __name__ == "__main__":
c = C(ks, vs)
print c.ab
但是cab返回一个属性对象。
用k = property(lambda x: vs[i])替换setattr行根本没有用。
那么,在运行时创建实例属性的正确方法是什么呢?
附注:我知道在如何使用__getattribute__方法中提出了一个替代方案?
如何动态地将属性添加到python类?
假设您有一个希望向其添加属性的对象。通常,当我需要开始管理对具有下游用途的代码中的属性的访问时,我希望使用属性,以便能够维护一致的API。现在,我通常会将它们添加到定义对象的源代码中,但让我们假设您没有这种访问权限,或者您需要真正地以编程方式动态地选择函数。
创建类
使用一个基于属性文档的例子,让我们创建一个具有“hidden”属性的对象类,并创建它的一个实例:
class C(object):
'''basic class'''
_x = None
o = C()
在Python中,我们期望有一种明显的做事方式。但是,在本例中,我将展示两种方法:使用装饰符符号和不使用装饰符符号。首先,没有装饰符号。对于getter、setter或delete的动态赋值,这可能更有用。
动态(又名猴子修补)
让我们为我们的类创建一些:
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
现在我们把这些赋值给性质。注意,我们可以在这里以编程方式选择函数,回答动态问题:
C.x = property(getx, setx, delx, "I'm the 'x' property.")
和用法:
>>> o.x = 'foo'
>>> o.x
'foo'
>>> del o.x
>>> print(o.x)
None
>>> help(C.x)
Help on property:
I'm the 'x' property.
修饰符
我们可以用上面的装饰符符号做同样的事情,但在这种情况下,我们必须将所有方法命名为相同的名称(我建议与属性保持相同),因此编程赋值并不像使用上述方法那样简单:
@property
def x(self):
'''I'm the 'x' property.'''
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
并将属性对象及其配置的setter和deleters赋值给类:
C.x = x
和用法:
>>> help(C.x)
Help on property:
I'm the 'x' property.
>>> o.x
>>> o.x = 'foo'
>>> o.x
'foo'
>>> del o.x
>>> print(o.x)
None
我最近遇到了一个类似的问题,我提出的解决方案使用__getattr__和__setattr__为我想要它处理的属性,其他一切都传递给原始。
class C(object):
def __init__(self, properties):
self.existing = "Still Here"
self.properties = properties
def __getattr__(self, name):
if "properties" in self.__dict__ and name in self.properties:
return self.properties[name] # Or call a function, etc
return self.__dict__[name]
def __setattr__(self, name, value):
if "properties" in self.__dict__ and name in self.properties:
self.properties[name] = value
else:
self.__dict__[name] = value
if __name__ == "__main__":
my_properties = {'a':1, 'b':2, 'c':3}
c = C(my_properties)
assert c.a == 1
assert c.existing == "Still Here"
c.b = 10
assert c.properties['b'] == 10
对于那些来自搜索引擎的人,这里是我在谈论动态属性时寻找的两件事:
class Foo:
def __init__(self):
# we can dynamically have access to the properties dict using __dict__
self.__dict__['foo'] = 'bar'
assert Foo().foo == 'bar'
# or we can use __getattr__ and __setattr__ to execute code on set/get
class Bar:
def __init__(self):
self._data = {}
def __getattr__(self, key):
return self._data[key]
def __setattr__(self, key, value):
self._data[key] = value
bar = Bar()
bar.foo = 'bar'
assert bar.foo == 'bar'
如果你想放置动态创建的属性,__dict__是很好的。__getattr__只适合在需要该值时执行某些操作,例如查询数据库。set/get组合有助于简化对存储在类中的数据的访问(如上面的示例)。
如果您只想要一个动态属性,请查看property()内置函数。
这里有一个解决方案:
允许将属性名指定为字符串,因此它们可以来自一些外部数据源,而不是全部列在程序中。
在定义类时添加属性,而不是每次创建对象时添加属性。
定义类之后,你只需动态地向它添加一个属性:
setattr(SomeClass, 'propertyName', property(getter, setter))
下面是一个完整的例子,用Python 3测试过:
#!/usr/bin/env python3
class Foo():
pass
def get_x(self):
return 3
def set_x(self, value):
print("set x on %s to %d" % (self, value))
setattr(Foo, 'x', property(get_x, set_x))
foo1 = Foo()
foo1.x = 12
print(foo1.x)