-1

In JavaScript this code returns 4:

let x = 3;

let foo = () => {
  console.log(x);
}

let bar = () => {
  x = 4;
  foo();
}

bar();

But same code in Python3 returns 3:

x = 3

def foo():
  print(x)

def bar():
  x = 4
  foo()

bar()

https://repl.it/@brachkow/python3scope

Why and how it works?

brachkow
  • 29
  • 6
  • Does this answer your question? [Use of "global" keyword in Python](https://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python) – dsasd Jul 25 '20 at 14:57
  • Possible duplicate: [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – Andreas Jul 25 '20 at 14:59

4 Answers4

1

To assign to the global x, you'll need to declare global x in the bar function.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • I don't want to assign global x because i don't want to overwrite top scope x. I want to overwrite x at bar() scope. – brachkow Jul 25 '20 at 15:04
  • 1
    @brachkoff _"i don't want to overwrite top scope x"_ - But that's what the JavaScript part does. – Andreas Jul 25 '20 at 15:23
1

If a variable name which is defined in the global scope and also used in a local scope of the function, two things happen:

  • You are making a read operation (Example: Simply printing it), then the value that variable reference is the same as the global object
x = 3

def foo():
  print(x)

foo()

# Here the x in the global scope and foo's scope both point to the same int object

  • You are making a write operation (Example: Assigning a value to the variable), then a new object is created with it reference in the function's local scope. This no longer points to the global object
x = 3

def bar():
  x = 4

bar()

# Here the x in the global scope and bar's scope points to two different int objects

However, if you want to use a variable from a global scope and want to make write operations on it inside a local scope, you need to declare it as global

x = 3

def bar():
  global x
  x = 4

bar()

# Both x points to the same int object
Atul
  • 598
  • 5
  • 12
0

use global keyword

x = 3
def foo:
    global x
    x = 4
    print(x)
foo()
Tushar Gautam
  • 275
  • 4
  • 9
0

It is very clear that, the program, the machine is working in the mapping

bar()

# in bar function you have x, but python takes it as a private x, not the global one
def bar():
  x = 4
  foo()

# Now when you call foo(), it will take the global x = 3 
# into consideration, and not the private variable [Basic Access Modal]
def foo():
   print(x)

# Hence OUTPUT
# >>> 3

Now, if you want to print 4, not 3, which is global one, you need to pass the private value inside the foo(), and make foo() accept an argument

def bar():
  x = 4
  foo(x)

def foo(args):
   print(args)

# OUTPUT
# >>> 4

OR

Use global inside your bar(), so that machine will understand that x inside in bar, is global x, not private

def bar():
  # here machine understands that this is global variabl
  # not the private one
  global x = 4
  foo()
Alok
  • 8,452
  • 13
  • 55
  • 93