1

I'm new to Python , i have the question here. I know the scope thing that whenever a variable is called , python looks into the current scope to see if it exists , if not then go out to the outer scope. Here's my question , i have a global variable x and two local variables as the same name in different functions. But the output shows that one local variable has the same id as the global one. I assume which confused me. If anyone could explain it to me i really appreciate!

 x = 10

def variablesTest():
    x = 10
    print(id(x))

def variablesTest2():
    x = 20
    print(id(x))

variablesTest()
variablesTest2()
print(id(x))

//output 1643250448 1643250768 1643250448

Renton Hsu
  • 11
  • 1
  • `id()` gives the identity of the *object*, that is the integer object with the value 10 or the integer object with the value 20. In python, names like `x` are only *references* to objects, not the objects themselves. – cdarke Dec 17 '16 at 13:00
  • Thanks man , you made it really clear. – Renton Hsu Dec 18 '16 at 01:23

0 Answers0