动态生成方法,给出字典的键
Generate methods dynamically giving the keys of a dictionary
正在寻找针对以下情况的解决方案(不确定是否存在!):
起点是字典
dict = {k<sub>1</sub>:v<sub>1</sub>, k<sub> 2</sub>:v<sub>2</sub>,...,k<sub>n</sub>:v<sub>n</sub>}
其中 n
未 固定。
有没有一种方法可以编写一个通用的 class,它将动态生成 n
方法,可以在以下示例中调用这些方法:
class 例子(字典):
example.k<sub>1</sub>()
example.k<sub>2</sub>()
.
.
.
example.k<sub>n</sub>()
每个
example.k<sub>i</sub>()
其中1<=i<=n
,应该return对应v<sub>i</sub>
.
你想要的是覆盖__getattr__
函数described here。
举个例子:
class example(dict):
def __getattr__(self, name):
return lambda: self[name]
这允许你做:
e = example()
e["foo"] = 1
print e.foo()
==> 1
与其动态创建这么多方法,不如重写 class 的 __getattr__
方法,并从那里调用 return 方法:
class Example(dict):
def __getattr__(self, k):
if k in self:
return lambda: self[k]
raise TypeError('Example object has not attribute {!r}'.format(k))
请注意,keys()
、items()
等键不会被调用,因为 __getattribute__
在 class 中找到了它们本身。最好不要以它们命名任何键。
演示:
>>> d = Example(a=1, b=2, c=3)
>>> d.a()
1
>>> d.b()
2
>>> d.foo()
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
d.foo()
File "/home/ashwini/py/so.py", line 7, in __getattr__
raise TypeError('Example object has not attribute {!r}'.format(k))
TypeError: Example object has not attribute 'foo'
我认为向 class 动态添加一个方法可以帮助您。
class example(object) :
dict={'k1':'v1','k2':'v2','k3':'v3','kn':'vn'}
def getvalue(self,key) :
return self.dict[key]
if __name__=="__main__" :
e = example()
e.method1=e.getvalue # this is adding a method to example class dynamically.
print e.method1('k1')
e.method2=e.getvalue
print e.method2('k2')
e.method3=e.getvalue
print e.method3('k3')
e.methodn=e.getvalue
print e.methodn('kn')
这个输出
v1
v2
v3
vn
正在寻找针对以下情况的解决方案(不确定是否存在!):
起点是字典
dict = {k<sub>1</sub>:v<sub>1</sub>, k<sub> 2</sub>:v<sub>2</sub>,...,k<sub>n</sub>:v<sub>n</sub>}
其中 n
未 固定。有没有一种方法可以编写一个通用的 class,它将动态生成
n
方法,可以在以下示例中调用这些方法:
class 例子(字典):
example.k<sub>1</sub>()
example.k<sub>2</sub>()
.
.
.
example.k<sub>n</sub>()
每个
example.k<sub>i</sub>()
其中1<=i<=n
,应该return对应v<sub>i</sub>
.
你想要的是覆盖__getattr__
函数described here。
举个例子:
class example(dict):
def __getattr__(self, name):
return lambda: self[name]
这允许你做:
e = example()
e["foo"] = 1
print e.foo()
==> 1
与其动态创建这么多方法,不如重写 class 的 __getattr__
方法,并从那里调用 return 方法:
class Example(dict):
def __getattr__(self, k):
if k in self:
return lambda: self[k]
raise TypeError('Example object has not attribute {!r}'.format(k))
请注意,keys()
、items()
等键不会被调用,因为 __getattribute__
在 class 中找到了它们本身。最好不要以它们命名任何键。
演示:
>>> d = Example(a=1, b=2, c=3)
>>> d.a()
1
>>> d.b()
2
>>> d.foo()
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
d.foo()
File "/home/ashwini/py/so.py", line 7, in __getattr__
raise TypeError('Example object has not attribute {!r}'.format(k))
TypeError: Example object has not attribute 'foo'
我认为向 class 动态添加一个方法可以帮助您。
class example(object) :
dict={'k1':'v1','k2':'v2','k3':'v3','kn':'vn'}
def getvalue(self,key) :
return self.dict[key]
if __name__=="__main__" :
e = example()
e.method1=e.getvalue # this is adding a method to example class dynamically.
print e.method1('k1')
e.method2=e.getvalue
print e.method2('k2')
e.method3=e.getvalue
print e.method3('k3')
e.methodn=e.getvalue
print e.methodn('kn')
这个输出 v1 v2 v3 vn