是否有方法將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