我的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)
有没有办法避免重复定义这些函数而不影响执行速度?