English 中文(简体)
是否用一个抽象的班子来进行活动手递解?
原标题:Is it idiomatic Python to use an abstract class for event handler callbacks?

我正在建立一个活动发送者框架,将电文编码化,重新输入用户代码。 我的C++背景表明,我写道:

class Handler:
    def onFoo(self):
        pass
    def onBar(self):
        pass

class Dispatcher:
    def __init__(self):
        self.handler = Handler()

    def run():
        while True:
            msg = decode_message() # magic
            if msg ==  foo :
                handler.onFoo()
            if msg ==  bar :
                handler.onBar()

然后,框架用户将撰写一些内容,如:

class MyHandler(Handler):
    def onFoo(self):
        do_something()

dispatcher = Dispatcher()
myHandler = MyHandler()
dispatcher.handler = myHandler
dispatcher.run()

但是,我也想把<代码>onFoo()和on Bar(>作为Displer的方法,让用户以其他方法取代。 然后,用户代码将考虑:

def onFoo():
    do_something()

dispatcher = Dispatcher()
dispatcher.onFoo = onFoo

我还可制作<代码>分机。 Foo

怎么做呢?

最佳回答

I wouldn t say that there s anything particularly wrong about doing it the first way, especially if you want to allow Dispatcher objects to be customized in an organized way at runtime (i.e. by passing different kinds of Handler objects to them), if your Handlers need to interact with and maintain complex state.

但是,你并没有真正获得任何好处,而是界定了基底<代码>Handler<>/code>;一类人仍然可以下级<代码>Handler<>/code>,而不会凌驾于任何方法之上,因为这是一个抽象的基础类别——它只是一个固定的基础类别。 因此,如果存在一些敏感的违约行为,我建议将其放在<代码>Handler上。 否则,用户根本不从<代码>Handler<>/code>中获取任何东西——他们也可以仅仅界定自己的<代码>Handler<>/code>。 虽然你只是想提供一无选择的持股人,但这种设置是罚款的。

不管怎样,我个人喜欢你建议的第二种方法;我并不肯定,要么更多的是“慢性”,但首先似乎喜欢对我采取更清洁的做法,因为它保留了<代码>Handler<>/code>和Disuper逻辑。 (它避免了你在<代码>上看到的情况。) 仅能安全地凌驾于、<_>>>>>>>>>> 方法之上的 我总是发现,有点jar。

但我觉得我应该指出,如果你真的想要一个bon光的抽象基类,你就应该写一个! 至2.6岁时,Khury公司支持灵活的abstract base category,有多种冰功能。 例如,你可以用<条码>载荷/代码>代号确定一种抽象的方法,以确保其must被子流压倒;但你还可以界定不一定要凌驾于次要地位的常规方法。 如果你重新寻找C++ idiom的灰色版本,这或许是最佳途径——当然,这并非同一件事,而是比你现在更加接近。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...