2024-05-14 10:50:11 发布
网友
我有一个函数定义如下:
def test(self, *args, wires=None, do_queue=True): pass
在Python3中,它正常运行,但在Python2中,它会因语法错误而崩溃。如何修改它以在Python2中工作?你知道吗
在Python2中实现这一点的唯一方法是将仅关键字参数作为**kwargs接受并手动提取它们。python2无法以任何其他方式执行仅关键字的参数;它是a new feature of Python 3 to allow this at all。你知道吗
**kwargs
最接近的Python 2等价物是:
def test(self, *args, **kwargs): wires = kwargs.pop('wires', None) do_queue = kwargs.pop('do_queue', True) if kwargs: raise TypeError("test got unexpected keyword arguments: {}".format(kwargs.keys()))
在Python2中实现这一点的唯一方法是将仅关键字参数作为
**kwargs
接受并手动提取它们。python2无法以任何其他方式执行仅关键字的参数;它是a new feature of Python 3 to allow this at all。你知道吗最接近的Python 2等价物是:
相关问题 更多 >
编程相关推荐