python,即时动态实现一个类

发布于 2024-10-06 07:46:28 字数 537 浏览 0 评论 0原文

假设我有一个实现多种方法的类。我们希望用户选择在现有方法中运行哪些方法,或者他可以决定添加任何 on_the_fly 方法。

从示例中

class RemoveNoise():
       pass

,可以根据需要添加方法,

RemoveNoise.raw = Raw()
RemoveNoise.bais = Bias()
etc

他甚至可以编写一个新方法

def new():
   pass

,并添加 new() 方法

RemoveNoise.new=new
run(RemoveNoise)

run() 是一个评估此类类的函数。

我想保存 class_with_the_methods_used 并将此类链接到创建的对象。

关于如何在 python 中解决这个问题有任何提示吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2024-10-13 07:46:28

可以在运行时将函数添加到类中。

class Foo(object):
  pass

def bar(self):
  print 42

Foo.bar = bar
Foo().bar()

Functions can be added to a class at runtime.

class Foo(object):
  pass

def bar(self):
  print 42

Foo.bar = bar
Foo().bar()
烟酒忠诚 2024-10-13 07:46:28

无需解决,您只需执行即可。这是您的代码,需要进行一些小的更改:

class RemoveNoise():
       pass

RemoveNoise.raw = Raw
RemoveNoise.bias = Bias

def new(self):
   pass

RemoveNoise.new=new

instance = RemoveNoise()

就这么简单。 Python 很棒。

不过,我到底为什么需要这个?

There is no solving needed, you just do it. Here is your code, with the small changes needed:

class RemoveNoise():
       pass

RemoveNoise.raw = Raw
RemoveNoise.bias = Bias

def new(self):
   pass

RemoveNoise.new=new

instance = RemoveNoise()

It's that simple. Python is wonderful.

Why on earth you would need this is beyond me, though.

此生挚爱伱 2024-10-13 07:46:28

好吧,这里有一些代码可以满足我认为您要求的功能 - 尽管我不太确定当您写“我想保存 class_with_the_methods_used”时“保存”的含义。另请注意,如果用户输入来自不受信任的来源,则对用户输入使用 exec 语句可能会极其危险。

import copy

# an empty "template" class
class Generic():
    pass

# predefined functions that create common methods
def Raw():
    def raw(self):
        print 'in Raw method of instance', id(self)
    return raw

def Bias():
    def bias(self):
        print 'in Bias method of instance', id(self)
    return bias

def user_new_function(definition):
    tempdict = {}
    exec definition in tempdict
    return tempdict['new']

# create a new class
RemoveNoise = copy.deepcopy(Generic)
RemoveNoise.__name__ = 'RemoveNoise' # change the class name of the copy

# add a couple of predefined methods
RemoveNoise.raw = Raw()
RemoveNoise.bias = Bias()

# add user defined 'new' method
user_new_def = """\
def new(self):
    print 'in user defined method "new" of instance', id(self)
"""
RemoveNoise.new = user_new_function(user_new_def)

# create and use an instance of dynamically defined class
instance = RemoveNoise()
print 'RemoveNoise instance "{}" created'.format(id(instance))
# RemoveNoise instance "11974736" created
instance.raw()
# in Raw method of instance 11974736
instance.bias()
# in Bias method of instance 11974736
instance.new()
# in user defined method "new" of instance 11974736

Well, here's some code that does what I think you're asking for -- although I'm not really sure what you meant by "save" when you wrote "I want to save the class_with_the_methods_used". Also note that using an exec statement on user input can be extremely dangerous if it comes from an untrusted source.

import copy

# an empty "template" class
class Generic():
    pass

# predefined functions that create common methods
def Raw():
    def raw(self):
        print 'in Raw method of instance', id(self)
    return raw

def Bias():
    def bias(self):
        print 'in Bias method of instance', id(self)
    return bias

def user_new_function(definition):
    tempdict = {}
    exec definition in tempdict
    return tempdict['new']

# create a new class
RemoveNoise = copy.deepcopy(Generic)
RemoveNoise.__name__ = 'RemoveNoise' # change the class name of the copy

# add a couple of predefined methods
RemoveNoise.raw = Raw()
RemoveNoise.bias = Bias()

# add user defined 'new' method
user_new_def = """\
def new(self):
    print 'in user defined method "new" of instance', id(self)
"""
RemoveNoise.new = user_new_function(user_new_def)

# create and use an instance of dynamically defined class
instance = RemoveNoise()
print 'RemoveNoise instance "{}" created'.format(id(instance))
# RemoveNoise instance "11974736" created
instance.raw()
# in Raw method of instance 11974736
instance.bias()
# in Bias method of instance 11974736
instance.new()
# in user defined method "new" of instance 11974736
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文