python,动态地实现一个类 [英] python, dynamically implement a class onthefly
本文介绍了python,动态地实现一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
来自示例
class RemoveNoise():
pass
然后按要求添加方法
RemoveNoise.raw = Raw()
RemoveNoise.bais =偏见()
等
他甚至可以写一个新的
def new():
pass
并添加 new()
方法
RemoveNoise.new = new
run(RemoveNoise)
run()
是一个函数,用于评估这样的类。
我想保存class_with_the_methods_used并将该类链接到创建的对象。 / p>
有关如何在python中解决这个问题的任何提示?
解决方案
函数可以在运行时添加到类中。
class Foo(object):
pass
def bar(self):
print 42
Foo.bar = bar
Foo().bar()
Assuming i have a class that implements several methods. We want a user to chose to which methods to run among the exisiting methods or he can decide to add any method on_the_fly.
from example
class RemoveNoise():
pass
then methods are added as wanted
RemoveNoise.raw = Raw()
RemoveNoise.bais = Bias()
etc
he can even write a new one
def new():
pass
and also add the new()
method
RemoveNoise.new=new
run(RemoveNoise)
run()
is a function that evaluates such a class.
I want to save the class_with_the_methods_used and link this class to the object created.
Any hints on how to solve this in python?
解决方案
Functions can be added to a class at runtime.
class Foo(object):
pass
def bar(self):
print 42
Foo.bar = bar
Foo().bar()
这篇关于python,动态地实现一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文