Passing an integer by reference in Python

后端 未结 11 1861
天命终不由人
天命终不由人 2020-11-22 12:29

How can I pass an integer by reference in Python?

I want to modify the value of a variable that I am passing to the function. I have read that everything in Python i

11条回答
  •  孤独总比滥情好
    2020-11-22 13:08

    Most cases where you would need to pass by reference are where you need to return more than one value back to the caller. A "best practice" is to use multiple return values, which is much easier to do in Python than in languages like Java.

    Here's a simple example:

    def RectToPolar(x, y):
        r = (x ** 2 + y ** 2) ** 0.5
        theta = math.atan2(y, x)
        return r, theta # return 2 things at once
    
    r, theta = RectToPolar(3, 4) # assign 2 things at once
    

提交回复
热议问题