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.

sertsedat
  • 3,490
  • 1
  • 25
  • 45
kur ag
  • 591
  • 8
  • 19
  • 1
    `a` is not copied to `d`. Lists are never copied unless you explicitly tell them to – Moberg Aug 29 '18 at 08:58
  • 2
    @Moberg this does not only apply to lists, it works the same for all types. – bruno desthuilliers Aug 29 '18 at 08:59
  • 1
    Possible duplicate of [Why can a function modify some arguments as perceived by the caller, but not others?](https://stackoverflow.com/questions/575196/why-can-a-function-modify-some-arguments-as-perceived-by-the-caller-but-not-oth) – tripleee Aug 29 '18 at 09: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. – bruno desthuilliers Aug 29 '18 at 09:07

2 Answers2

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.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • 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."(https://nedbatchelder.com/text/names1.html) Thanks @bruno – kur ag Aug 29 '18 at 12:32
-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 '='.

RDoc
  • 346
  • 1
  • 10
  • "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` – bruno desthuilliers Aug 29 '18 at 09:13