如何根据类别调度对象 [英] how to dispatch objects depending on their class

查看:64
本文介绍了如何根据类别调度对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



大家好。


关于以下情况,我有几个问题:


class A(对象):

def __init __(个体经营):

传递


B级(对象):

def __init __(自我):

A .__ init __(自我)


def func(对象):

if isinstance(object,A):

do_something_with_A(object)

elif isinstance(object,B):

do_something_with_B(object)


请注意,在我的实际问题中,我无法将func的逻辑移动到

类A和B,因为我将为func设置层次结构。然后我需要

一种方法将对象分派给正确的函数。我想用

使用字典,例如:


FUNC = {"< class''__ main __。A''>":do_something_with_A ,

"< class''__ main __。B''>":do_something_with_B}


def func(object):

FUNC [type(object)](object)


但是:(1)我不确定类型(对象)函数返回的是什么。在这个

的情况下,A和B在__main__命名空间中(假设这是如何调用

),但如果它们在模块中; (2)我不确定它是否有效b / b
;最后(3):可能有一个更好的方法来做它

(这总是一个安全的假设)。


我希望我的问题很明确,对不起,如果我问的是

这是常识,但我甚至不知道该怎么去谷歌...


谢谢,curzio


Hi all.

I have a couple of question regarding the following situation:

class A(object):
def __init__(self):
pass

class B(object):
def __init__(self):
A.__init__(self)

def func(object):
if isinstance(object, A):
do_something_with_A(object)
elif isinstance(object, B):
do_something_with_B(object)

Note that in my real problem I cannot move the logic of func to the
class A and B because I will have a hierarchy also for func. Then I need
a way to dispatch the object to the right function. I thought about
using a dictionary, like:

FUNC = {"<class ''__main__.A''>": do_something_with_A,
"<class ''__main__.B''>": do_something_with_B}

def func(object):
FUNC[type(object)](object)

But: (1) I am not sure of what the type(object) function return. In this
case A and B are in the __main__ namespace (assuming this is how is
called), but if they are in a module; (2) I am not sure if it is
efficient; and finally (3): probably there is a better way to do it
(this is always a safe assumption).

I hope my problem is clear, and excuse me if I am asking about something
that is common knowledge, but I don''t even know what to google for...

thanks, curzio

推荐答案

Curzio Basso写道:
Curzio Basso wrote:
FUNC = {"< class'' __main __。一个''>":do_something_with_A,
"< class''__ main __。B''>":do_something_with_B}

def func(object):< br> FUNC [type(object)](object)


你必须稍微修改它才能工作。这也将非常有效。


func_dict = {A:do_something_with_A,

B:do_something_with_B}

def func(obj):

func_dict [obj .__ class __](obj)


使用类作为键避免了你担心的歧义。开销将是

一个字典查找和一个函数调用。这个简单的
方法的主要缺点是它不会搜索继承树但坚持

完全类匹配。

我希望我的问题很明确,请原谅我,如果我问的是一些常识,但我甚至都不知道该怎么做...
FUNC = {"<class ''__main__.A''>": do_something_with_A,
"<class ''__main__.B''>": do_something_with_B}

def func(object):
FUNC[type(object)](object)
You''ll have to modify that slightly for it to work. It will be very
efficient, too.

func_dict = {A: do_something_with_A,
B: do_something_with_B}
def func(obj):
func_dict[obj.__class__](obj)

Using classes as keys avoids the ambiguity you fear. The overhead will be
one dictionary lookup and a function call. The main drawback of this simple
approach is that it does not search the inheritance tree but insists on an
exact class match.
I hope my problem is clear, and excuse me if I am asking about something
that is common knowledge, but I don''t even know what to google for...




也许


python泛型发送


彼得



Maybe

python generic dispatch

Peter


它应该使用实际的类对象作为

dict中的键和作为值的函数不是一个问题。无需将类转换为

字符串。


那么为什么它们不能成为类的静态方法呢?这将更简单,但是dict会有效地运作。


-Casey


星期二,10 2004年8月16:35:48 +0200

Curzio Basso< cu ********** @ unibas.ch>写道:
It should not be a problem to use the actual class object as keys in the
dict and the functions as values. No need to convert the class to
strings.

So why can''t they be static methods of the classes exactly? That would
be simpler, however the dict will work efficiently.

-Casey

On Tue, 10 Aug 2004 16:35:48 +0200
Curzio Basso <cu**********@unibas.ch> wrote:

大家好。

关于以下情况,我有几个问题:

A班(对象):
def __init __(自我):
传递

B类(对象):
def __init __(自我):
A .__ init__ (自我)

def func(对象):
如果isinstance(对象,A):
do_something_with_A(对象)
elif isinstance(对象,B):
do_something_with_B(对象)

请注意,在我的实际问题中,我无法将func的逻辑移动到类A和B,因为我将为func提供层次结构。然后我需要一种方法将对象分派到正确的功能。我想
关于使用字典,例如:

FUNC = {"< class''__ main __。A''>":do_something_with_A,
"< ; class''_ _main __。B''>":do_something_with_B}

def func(object):
FUNC [type(object)](object)
的方式),但如果它们在模块中; (2)我不确定它是否有效;最后(3):可能有一个更好的方法来做到这一点
(这总是一个安全的假设)。

我希望我的问题很明确,如果我问的话请原谅我关于
一些常识,但我甚至不知道该怎样谷歌...

谢谢,curzio
-
http://mail.python.org/mailman/listinfo/python -list






Curzio Basso写道:
Curzio Basso wrote:

大家好。

关于以下情况我有几个问题:

A类(对象):
def __init __(自我):
传递

B类(对象):
def __init __(自我):
A .__ init __(self)

def func(对象):
if isinstance (对象,A):
do_something_with_A(对象)
elif isinstance(对象,B):
do_something_with_B(对象)
请注意,在我的实际问题中,我无法将func的逻辑移动到类A和B,因为我将为func提供层次结构。然后我需要一种方法将对象发送到正确的函数。

Hi all.

I have a couple of question regarding the following situation:

class A(object):
def __init__(self):
pass

class B(object):
def __init__(self):
A.__init__(self)

def func(object):
if isinstance(object, A):
do_something_with_A(object)
elif isinstance(object, B):
do_something_with_B(object)

Note that in my real problem I cannot move the logic of func to the
class A and B because I will have a hierarchy also for func. Then I need
a way to dispatch the object to the right function.




我在Python中看到类似于多方法实现的东西,

这可能会让你感兴趣。
http://www-106.ibm.com/developerwork.../l-pydisp.html



I saw something like a multi-methods implementation in Python somewhere,
this may interest you.
http://www-106.ibm.com/developerwork.../l-pydisp.html


这篇关于如何根据类别调度对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆