what is the purpose of scopes in python as determined by indentation

Jimm

In python 2.7.5 on mac os-x, i can wrote following lines. My question is that the variable age1 is declared inside an inner block and hence should not be visible in the subsequent outer block. But python compiles and does not complain. If i comment the line age1 = 25, then python complains that the variable age1 is not declared.

In other languages like java/c++, variables declared within a scope (determined by {}) are not visible out side the scope. So my question is what is the purpose of scope in python as determined by indentation.

age = 41
if age >= 41:
 print(">=41")
 age1 = 25 #this is declared inside the scope of print and should not be visible outside
if age1 == 25:
 print("==25")
thefourtheye

Following from this excellent answer, there are only four scopes in Python.

LEGB Rule.

L. Local. (Names assigned in any way within a function (def or lambda)), and not declared global in that function.

E. Enclosing function locals. (Name in the local scope of any and all enclosing functions (def or lambda), form inner to outer.

G. Global (module). Names assigned at the top-level of a module file, or declared global in a def within the file.

B. Built-in (Python). Names preassigned in the built-in names module : open,range,SyntaxError,...

As you can see, based on indentation/control block, python doesn't limit the visibility of the variables.

So, if that code is within a function, then the variable will be visible to all parts of the code within that function, after its creation.

If that code is within a module, then the variable will be visible to all parts of the module, after its creation.

When you try to access a variable, first the local scope will be searched, if not found then the closure scope will be searched, if not found then module scope will be searched, if not found builtin scope will be searched, and if not found error will be thrown.

Check this function now,

def func(x):
    if x == 0: y = 1
    print locals()

func(1)
func(0)

Output

{'x': 1}
{'y': 1, 'x': 0}

In the first case, y is not created and so it is not there in local scope. But in the second case, y is added to the local scope, so, it will be visible to all the code which share the local scope.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the purpose of meshgrid in Python / NumPy?

From Java

What is the purpose of the single underscore "_" variable in Python?

From Java

Docker, what is it and what is the purpose

From Dev

Printing with indentation in python

From Dev

What is the purpose of checking self.__class__ ? - python

From Dev

What's the purpose of the "__package__" attribute in Python?

From Dev

Python - What is the purpose of defining the maximum string length in an SQLAlchemy model?

From Dev

What is the purpose of dollar sign in this Python3 string formatting expression?

From Dev

What is the purpose of .*\\?

From Dev

What is the purpose of this if statement from "Learning Python the Hard Way"?

From Dev

What is the purpose of compares in indices in Python?

From Dev

What is the purpose of a context manager in python

From Dev

what is the purpose of db=0 when connecting to redis server in python?

From Dev

What programming languages are scopes written in?

From Dev

What are Smart Scopes?

From Dev

What is the correct certificate purpose for SSL client in python?

From Dev

What's the purpose of giving an alias to an builtin function in Python

From Dev

What programming languages are scopes written in?

From Dev

What are Smart Scopes?

From Dev

Python: Indentation

From Dev

Python - What is the purpose of defining the maximum string length in an SQLAlchemy model?

From Dev

What is the purpose of [] operator while opening a file in python

From Dev

What is the purpose of "?"

From Dev

What is the GNOME equivalent of Unity scopes?

From Dev

what is the purpose of db=0 when connecting to redis server in python?

From Dev

Indentation on python

From Dev

Scopes in Python Modules

From Dev

In Python, what is the purpose of a trailing comma in a return statement?

From Dev

SpringBoot OAuth2: Purpose of the .scopes() function?

Related Related

  1. 1

    What is the purpose of meshgrid in Python / NumPy?

  2. 2

    What is the purpose of the single underscore "_" variable in Python?

  3. 3

    Docker, what is it and what is the purpose

  4. 4

    Printing with indentation in python

  5. 5

    What is the purpose of checking self.__class__ ? - python

  6. 6

    What's the purpose of the "__package__" attribute in Python?

  7. 7

    Python - What is the purpose of defining the maximum string length in an SQLAlchemy model?

  8. 8

    What is the purpose of dollar sign in this Python3 string formatting expression?

  9. 9

    What is the purpose of .*\\?

  10. 10

    What is the purpose of this if statement from "Learning Python the Hard Way"?

  11. 11

    What is the purpose of compares in indices in Python?

  12. 12

    What is the purpose of a context manager in python

  13. 13

    what is the purpose of db=0 when connecting to redis server in python?

  14. 14

    What programming languages are scopes written in?

  15. 15

    What are Smart Scopes?

  16. 16

    What is the correct certificate purpose for SSL client in python?

  17. 17

    What's the purpose of giving an alias to an builtin function in Python

  18. 18

    What programming languages are scopes written in?

  19. 19

    What are Smart Scopes?

  20. 20

    Python: Indentation

  21. 21

    Python - What is the purpose of defining the maximum string length in an SQLAlchemy model?

  22. 22

    What is the purpose of [] operator while opening a file in python

  23. 23

    What is the purpose of "?"

  24. 24

    What is the GNOME equivalent of Unity scopes?

  25. 25

    what is the purpose of db=0 when connecting to redis server in python?

  26. 26

    Indentation on python

  27. 27

    Scopes in Python Modules

  28. 28

    In Python, what is the purpose of a trailing comma in a return statement?

  29. 29

    SpringBoot OAuth2: Purpose of the .scopes() function?

HotTag

Archive