即时向类中添加方法 [英] Adding method to a class on the fly
问题描述
嗨列表,
我需要动态创建类方法。例如,如果我
做:
类假人:
def __init __(自我):
exec''''''def method_dynamic(self):\ n \treturn
self.method_static(它是我)''''''
返回
def method_static(自我,文字):
打印文本
返回
我喜欢和以下一样:
class Dummy:
def __init __(self):
返回
def method_dynamic(个体经营):
返回self.method_static(它就是我)
def method_static(自我,文字):
打印文本
返回
以便我可以做:
dum = Dummy.method_dynamic()
并看到它是我打印。
可以这样做吗?
谢谢,
Hi list,
I have a need to create class methods on the fly. For example, if I
do:
class Dummy:
def __init__(self):
exec ''''''def method_dynamic(self):\n\treturn
self.method_static("it''s me")''''''
return
def method_static(self, text):
print text
return
I like that to be the same as:
class Dummy:
def __init__(self):
return
def method_dynamic(self):
return self.method_static("it''s me")
def method_static(self, text):
print text
return
so that I can do:
dum=Dummy.method_dynamic()
and see "it''s me" printed.
Can that be done?
Thanks,
推荐答案
在网上发现一条消息,说我需要使用setattr在运行时向该类添加
方法。但是我该怎么做?
问候,
6月22日下午12:02,John Henry< john106he ...... @ hotmail.comwrote:
Found a message on the web that says I need to use setattr to add the
method to the class at run time. But how do I do that?
Regards,
On Jun 22, 12:02 pm, John Henry <john106he...@hotmail.comwrote:
嗨列表,
我需要动态创建类方法。例如,如果我
做:
类假人:
def __init __(自我):
exec''''''def method_dynamic(self):\ n \treturn
self.method_static(它是我)''''''
返回
def method_static(自我,文字):
打印文本
返回
我喜欢和以下一样:
class Dummy:
def __init __(self):
返回
def method_dynamic(个体经营):
返回self.method_static(它就是我)
def method_static(自我,文字):
打印文本
返回
以便我可以做:
dum = Dummy.method_dynamic()
并看到它是我打印。
可以这样做吗?
谢谢,
Hi list,
I have a need to create class methods on the fly. For example, if I
do:
class Dummy:
def __init__(self):
exec ''''''def method_dynamic(self):\n\treturn
self.method_static("it''s me")''''''
return
def method_static(self, text):
print text
return
I like that to be the same as:
class Dummy:
def __init__(self):
return
def method_dynamic(self):
return self.method_static("it''s me")
def method_static(self, text):
print text
return
so that I can do:
dum=Dummy.method_dynamic()
and see "it''s me" printed.
Can that be done?
Thanks,
6月22日下午3:02,John Henry< john106he ... @ hotmail.comwrote:
On Jun 22, 3:02 pm, John Henry <john106he...@hotmail.comwrote:
嗨列表,
我需要动态创建类方法。例如,如果我
做:
类假人:
def __init __(自我):
exec''''''def method_dynamic(self):\ n \treturn
self.method_static(它是我)''''''
返回
def method_static(自我,文字):
打印文本
返回
我喜欢和以下一样:
class Dummy:
def __init __(self):
返回
def method_dynamic(个体经营):
返回self.method_static(它就是我)
def method_static(自我,文字):
打印文本
返回
以便我可以做:
dum = Dummy.method_dynamic()
并看到它是我打印。
这可以吗?
谢谢,
Hi list,
I have a need to create class methods on the fly. For example, if I
do:
class Dummy:
def __init__(self):
exec ''''''def method_dynamic(self):\n\treturn
self.method_static("it''s me")''''''
return
def method_static(self, text):
print text
return
I like that to be the same as:
class Dummy:
def __init__(self):
return
def method_dynamic(self):
return self.method_static("it''s me")
def method_static(self, text):
print text
return
so that I can do:
dum=Dummy.method_dynamic()
and see "it''s me" printed.
Can that be done?
Thanks,
class假人:
def方法(self,arg):
print arg
def method2(self,arg):
self.method(arg)
Dummy.method2 = method2
Dummy.method2(''Hello,world!'')
class Dummy:
def method(self, arg):
print arg
def method2(self, arg):
self.method(arg)
Dummy.method2 = method2
Dummy.method2(''Hello, world!'')
6月22日下午2:24,askel< dummy ... @ mail.ruwrote:
On Jun 22, 2:24 pm, askel <dummy...@mail.ruwrote:
class Dummy:
def方法(self,arg):
print arg
def method2( self,arg):
self.method(arg)
Dummy.method2 = method2
Dummy.method2('' Hello,world!'')
class Dummy:
def method(self, arg):
print arg
def method2(self, arg):
self.method(arg)
Dummy.method2 = method2
Dummy.method2(''Hello, world!'')
Traceback(最近一次调用最后一次):
文件" test1.py",第8行,在?
Dummy.method2(''Hello,world!'')
TypeError:必须使用Dummy实例调用unbound方法method2()
作为第一个参数(得到str实例
代替)
Traceback (most recent call last):
File "test1.py", line 8, in ?
Dummy.method2(''Hello, world!'')
TypeError: unbound method method2() must be called with Dummy instance
as first argument (got str instance
instead)
>我喜欢它与以下相同:
class Dummy:
def __init __(个体经营):
返回
def method_dynamic(个体经营):
返回自我.method_static(它就是我)
def method_static(self,text):
打印文本
返回
以便我可以这样做:
dum = Dummy.method_dynamic()
并看到它是我的打印。
>I like that to be the same as:
class Dummy:
def __init__(self):
return
def method_dynamic(self):
return self.method_static("it''s me")
def method_static(self, text):
print text
return
so that I can do:
dum=Dummy.method_dynamic()
and see "it''s me" printed.
您什么时候能看到?
When are you able to see that?
这篇关于即时向类中添加方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!