-2

The title is not the issue. this issue i already solved by understanding the "immutable and mutable" -thing. But, I wanted to know a bit better about it. What are exactly allow the object to be acceced by another function if i dont tuch him and make shadow of the first one created. I show you with code:

def text():
    print x

x = 6
text()

This example whill work well, because i didnt created a new object of immutable. but this:

def text():
    print x

text()
x = 6

This will not work because the integer is not a global variable.

What is exactly happend when i created a new object without globaled it? to where in the memory this objects goes that allowed me to approach to it by another scope. why its so different then just a global?

dfsfg sfg
  • 77
  • 1
  • 9
  • 1
    Since Python code is interpreted rather than compiled, the variable x never happened when the text() is called in the second example. That's the reason it never happened. – thiruvenkadam Sep 19 '16 at 07:23

1 Answers1

0

Inside a function, if you assign to a variable and don't use global on it, then that variable is local. If you only use a variable and not assign to it, it is assumed to come from an outer scope, in this case from module level.

So that is why the first one uses the module level variable.

The second doesn't work because when text() is called, the variable at module level doesn't exist yet.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79