Modifying function arguments

后端 未结 3 1267
鱼传尺愫
鱼传尺愫 2020-11-30 14:29

Sorry if this is a dumb question, but I\'ve looked for a while and not really found the answer.

If I\'m writing a python function, for example:

def f         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 14:54

    you can do:

    def function(in1, in2):
       return  in1 + 1 , in2 + 1
    
    a, b = function(a,b)
    c, d = function(c,d)
    

    python functions are closed -> when function(a,b) s called, a and b get reassigned to a local (to the function) references/pointers in1 and in2, which are not accessible outside of the function. provide references to those new values w/o using globals, you will need to pass that back through return.

    When you pass an array or non primitive object into a function, you can modify the object's attributes and have those modifications be visible to other references for that object outside, because the object itself contain the pointers to those values, making the visible to anything else holding a pointer to that object.

提交回复
热议问题