logo CBCE Skill INDIA

Welcome to CBCE Skill INDIA. An ISO 9001:2015 Certified Autonomous Body | Best Quality Computer and Skills Training Provider Organization. Established Under Indian Trust Act 1882, Govt. of India. Identity No. - IV-190200628, and registered under NITI Aayog Govt. of India. Identity No. - WB/2023/0344555. Also registered under Ministry of Micro, Small & Medium Enterprises - MSME (Govt. of India). Registration Number - UDYAM-WB-06-0031863

What are scopes in Python?


Scopes in Python

In Python, a scope is a region of a program where a namespace is directly accessible. A namespace is a container that holds a collection of names (variables, functions, classes, etc.) and provides a mapping from names to objects. Scopes help in organizing and managing namespaces in a Python program.

 

There are two main types of scopes in Python:

 

  1. Local Scope:

    • A local scope is the innermost scope and is created when a function is called.
    • It contains the names defined within the function and is only accessible from within that function.
    • Local variables are variables defined inside a function and are limited to that function's scope.
    def my_function():
        x = 10  # Local variable
        print(x)
    
    my_function()
    # print(x)  # This would result in an error because x is not defined in this scope
  2. Global Scope:

    • The global scope is the outermost scope and is created when the program starts.
    • It contains names that are defined at the top level of a script or module, outside of any function.
    • Global variables are accessible from anywhere in the code, including within functions.
    y = 20  # Global variable
    
    def another_function():
        print(y)  # Accessing the global variable
    
    another_function()
    • It's important to note that if a variable is assigned a value inside a function without the global keyword, Python assumes it is a local variable, even if a variable with the same name exists in the global scope. To modify a global variable from within a function, the global keyword is used.
    z = 30  # Global variable
    
    def modify_global():
        global z
        z = 40
        print(z)
    
    modify_global()
    print(z)  # The global variable has been modified
  3. Enclosing (or Nonlocal) Scope:

    • This scope is relevant when dealing with nested functions (a function inside another function).
    • It refers to the scope of the containing (enclosing) function.
    • Names in the enclosing scope are accessible to the inner function, but not vice versa.
    def outer_function():
        a = 50  # Enclosing variable
    
        def inner_function():
            print(a)  # Accessing the enclosing variable
    
        inner_function()
    
    outer_function()

 

Understanding and managing variable scopes is crucial for writing correct and maintainable Python code. It helps prevent naming conflicts, ensures proper variable access, and contributes to the overall structure of the program.

 

Thank you.

Popular Post:

Give us your feedback!

Your email address will not be published. Required fields are marked *
0 Comments Write Comment