call-by-reference function parameters

后端 未结 4 1054
挽巷
挽巷 2021-01-15 02:03

Given a function:

def A(a, b, c):
    a *= 2
    b *= 4
    c *= 8
    return a+b+c

How can I set the \'c\' var to be called-by-reference,

4条回答
  •  抹茶落季
    2021-01-15 02:18

    All calls in Python are "by reference". Integers are immutable in Python. You can't change them.

    class C:
       def __init__(self, c):
           self.c = c
       def __call__(self, a, b):
           a *= 2
           b *= 4
           self.c *= 8
           return a + b + self.c
    

    Example

    A = C(1)
    print A(1, 1), A.c
    print A(1, 1), A.c
    

    Output

    14 8
    70 64
    

提交回复
热议问题