WHAT IS SCOPE RULES IN PYTHON PROGRAMMING

SCOPE RULES

Each identifier has a scope that determines where you can use it in your program. For that portion of the program, the identifier is said to be “in scope.”

Local Scope

A local variable’s identifier has local scope. It’s “in scope” only from its definition to the end of the function’s block. It “goes out of scope” when the function returns to its caller. So, a local variable can be used only inside the function that defines it.

Global Scope

Identifiers defined outside any function (or class) have global scope—these may include functions, variables and classes. Variables with global scope are known as global variables. Identifiers with global scope can be used in a .py file or interactive session anywhere after they’re defined.

Accessing a Global Variable from a Function

You can access a global variable’s value inside a function:

view code image

In [1]: x = 7
In [2]: def access_global():
...: print('x printed from access_global:', x)
...:
In [3]: access_global()
x printed from access_global: 7

However, by default, you cannot modify a global variable in a function—when you first assign a value to a variable in a function’s block, Python creates a new local variable:

view code image

In [4]: def try_to_modify_global():
...: x = 3.5
...: print('x printed from try_to_modify_global:', x)
...:
In [5]: try_to_modify_global()
x printed from try_to_modify_global: 3.5
In [6]: x
Out[6]: 7

In function try_to_modify_global’s block, the local x shadows the global x, making it inaccessible in the scope of the function’s block. Snippet [6] shows that global variable x still exists and has its original value (7) after function try_to_modify_global executes.
To modify a global variable in a function’s block, you must use a global statement to declare that the variable is defined in the global scope:

view code image

In [7]: def modify_global():
...: global x
...: x = 'hello'
...: print('x printed from modify_global:', x)
...:
In [8]: modify_global()
x printed from modify_global: hello
In [9]: x
Out[9]: 'hello'

Blocks vs. Suites

You’ve now defined function blocks and control statement suites. When you create a variable in a block, it’s local to that block. However, when you create a variable in a control statement’s suite, the variable’s scope depends on where the control statement is defined:

  • If the control statement is in the global scope, then any variables defined in the control statement have global scope.
  • If the control statement is in a function’s block, then any variables defined in the control statement have local scope.

We’ll continue our scope discussion in the “Object­Oriented Programming” chapter when we introduce custom classes.

Shadowing Functions

In the preceding chapters, when summing values, we stored the sum in a variable named total. The reason we did this is that sum is a built­in function. If you define a variable named sum, it shadows the built­in function, making it inaccessible in your code. When you execute the following assignment, Python binds the identifier sum to the int object containing 15. At this point, the identifier sum no longer references the built­in function. So, when you try to use sum as a function, a TypeError occurs:

view code image

In [10]: sum = 10 + 5
In [11]: sum
Out[11]: 15
In [12]: sum([10, 5])
TypeError Traceback (most recent call last
)<
ipython-­input­-1-2­1237d97a65fb> in <module>()
> 1 sum([10, 5])
TypeError: 'int' object is not callable

Statements at Global Scope

In the scripts you’ve seen so far, we’ve written some statements outside functions at the global scope and some statements inside function blocks. Script statements at global scope execute as soon as they’re encountered by the interpreter, whereas statements in a block execute only when the function is called.

*

إرسال تعليق (0)
أحدث أقدم