小编Dan*_*ert的帖子

如何避免Python中类似的函数定义?

我的Python代码中有很多非常相似的函数,如下所示:

def callback1(msg):
    m = MsgX(msg)
    a = msg_type_a()
    a.x = m.x
    pub2.publish(a)

def callback2(msg):
    m = MsgY(msg)
    b = msg_type_b()
    b.t = m.t
    b.u = m.u
    pub2.publish(b)

# ... and a couple more in the same fashion
Run Code Online (Sandbox Code Playgroud)

有没有办法避免重复定义这些函数而不影响执行速度?

python function

2
推荐指数
1
解决办法
147
查看次数

标签 统计

function ×1

python ×1