是否有方法将variable number of arguments传递给函数,并使用(*args, **keywords)
参数传递样式更改这些参数?我试了几件事情,但无论是看不出有任何变更,或由编译器产生错误:Python:具有可变参数列表的对象分配
def foo(*args):
args[0] = 4
这让我TypeError: object does not support assignment
(他们的元组。)或者
def foo(*args):
plusOne = [ item+1 for item in args ]
args = plusOne
它没有
效果如此如此。如果没有机制也不能解决问题,我可以承认失败。
编辑:
解释,为什么我试图走这条路线,这里考虑的情况:
class bar(object):
def __init__(self,obj):
self.obj = obj
def foo(input):
input.obj = "something else"
如果我通过我的bar
对象到富,我得到了状态的变化。要创建一个装饰器来执行重置所有这种状态的deepcopy
,我目前正在为它定义N个参数。我想创建一个接受任意数量参数的函数。因此,这个问题。
相关:http://stackoverflow.com/questions/575196/in -python-why-can-a-function-modify-some-arguments-as-perceived-by-the-caller-b – SilentGhost 2010-06-22 13:51:13
感谢您的链接。 – wheaties 2010-06-22 15:50:21