0

I couldn't understand global and local variables in python, especially functions. I look lots of examples, but all of them don't explain in function calls clearly. My example code is below:

def called(d):
    del d[0]
    b=[1]
    return b

def main():
    a=[0,1,2,3]
    print("first ", a)
    c=called(a)
    print("second ", a)

main()

Output:

first  [0, 1, 2, 3]
second  [1, 2, 3]

I expect that "a" is local variable at main. When I call the "called" function a is copyed to d. And d is local at "called". But reality is different. I solve the problem with a "called(a.copy)". But I want to understand the logic.

4
  • 1
    a is not copied to d. Lists are never copied unless you explicitly tell them to Commented Aug 29, 2018 at 8:58
  • 2
    @Moberg this does not only apply to lists, it works the same for all types. Commented Aug 29, 2018 at 8:59
  • 1
    Possible duplicate of Why can a function modify some arguments as perceived by the caller, but not others? Commented Aug 29, 2018 at 9:05
  • 1
    @Moberg I repeat: this does not only apply to lists, it works the same for all types, including ints. You can easily test it out by yourself by check your int's id() both outside and inside the function. Commented Aug 29, 2018 at 9:07

2 Answers 2

2

Python never implicitely copies anything, when you pass an object (and everything in Python is an object) to a function what you have in the function IS the object you passed in, so if you mutate it the change will be seen outside the function.

OTHO, parameters names are local to the function so rebinding a parameter within a function only change what object this name points to within the function, it has no impact on the object passed.

For more in-depth explanation the best reference is Ned Batchelder's article here.

Sign up to request clarification or add additional context in comments.

1 Comment

In summary: "Just as names have no type, values have no scope. When we say that a function has a local variable, we mean that the name is scoped to the function: you can’t use the name outside the function, and when the function returns, the name is destroyed. But as we’ve seen, if the name’s value has other references, it will live on beyond the function call. It is a local name, not a local value."(nedbatchelder.com/text/names1.html) Thanks @bruno
-1

Basically, a global variable is one that can be accessed anywhere, regardless of whether or not it is in a function (or anything else).

A local variable is one that solely exists within the function in question. You cannot declare or access it anywhere else. You could, however, make it global by explicitly including that in the function.

As far as I can see,'d' has not really been defined as a variable, but as a function parameter, as seen in called(d). This could've been changed to called(blabla) and your function would behave the same exact way if you, within the function, also changed d[0] to blabla[0].

What this means is that when you call that function, anything that has 'd' in it would be replaced by what you're calling it with. In this case, the parameter has been changed to the variable a, and the functions are then executed as you've stated.

In order to define a variable, you have to use '='.

1 Comment

"As far as I can see,'d' has not really been defined as a variable, but as a function parameter" => function parameters ARE local variables. This is documented, and you can check it out by yourself by inspecting the function's .func_code.co_varnames

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.