Is there a method to pass a variable number of arguments to a function and have it change those arguments using the ( *args, **keywords ) style of argument passing? I’ve tried a few things but either see no change or have an error raised by the compiler:
def foo( *args ):
args[0] = 4
This gets me TypeError: object does not support assignment (they’re tuples.) Or
def foo( *args ):
plusOne = [ item+1 for item in args ]
args = plusOne
which has no effect what so ever. If there is no mechanism nor work around I can admit defeat.
Edit:
To clarify why I’m trying to go this route, consider the case here:
class bar(object):
def __init__(self,obj):
self.obj = obj
def foo( input ):
input.obj = "something else"
If I pass my bar object into foo, I get a change in the state. To create a decorator which performs a deepcopy which resets all such state I’m currently customizing it for N arguments. I’d like to create one which accepts any number of arguments. Hence, the question.
No – Python uses call by object-sharing, also known as call-by-value.
To clarify the terminology: you are not receiving a deep copy of the object, but a copy of the object reference. Note: this is not the same as call-by-reference! You can think of it as call by value, and that the values are references to objects.
So to answer your question, you receive a copy of the arguments (object references). You cannot modify the object references as if they were passed by reference. You can make a new modified copy of them if you want, but judging from your examples that isn’t what you are looking for. The calling scope won’t see your changes.
If instead you mutate the objects you receive, the client can see those changes.