Python: assigning an object using a list of variable arguments

Is there a way to pass a variable number of arguments to a function and change it using ( *args, **keywords )the argument passing style ? I tried several things, but either I don’t see the changes, or it has an error caused by the compiler:

def foo( *args ):
    args[0] = 4

It gets me TypeError: object does not support assignment(these are tuples.) Or

def foo( *args ):
    plusOne = [ item+1 for item in args ]
    args = plusOne

which has no effect. If there is no mechanism and does not work, 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"

bar foo, . , deepcopy, , N . , . , .

+3
3

- Python , .

: , . : , call-by-reference! .

, , ( ). , . , , , , , . .

, , .

+4

args[0] = 4

, , , args , . , , , :

>>> def foo( *args ):
    print(args)
    args = list(args)
    args[0] = 42
    print(args)


>>> foo(23)
(23,)
[42]

, , , , . , .

, :

>>> def spam(*a):
    a[0][0] = 42


>>> l = [23, 32]
>>> spam(l)
>>> l
[42, 32]

: l. :

>>> def foo( *input ):
    input[0].obj = "something else"


>>> b = bar('abc')
>>> foo(b)
>>> b.obj
'something else'
+3

, , - :

>>> x, y = (10, 17)
>>> foo(x, y)
>>> print (x, y)
(11, 18)

, , .

, , foo:

def foo(*args):
    for arg in args:
        arg['value'] += 1

>>> d1 = {'value': 1}
>>> d2 = {'value': 2}
>>> foo(d1, d2)
>>> print (d1, d2)
({'value': 2}, {'value': 3})
+1
source

Source: https://habr.com/ru/post/1751201/


All Articles