Python Scope

Python Scope

In Python, scope refers to the region in a program where a particular variable is accessible and can be referenced. It defines the visibility and lifetime of variables and other named entities in your code.

Python has the following types of scopes:

  1. Local scope: Variables defined within a function have a local scope. They are only accessible within that specific function. Once the function execution is complete, the local variables are destroyed. Local variables can shadow variables with the same name in outer scopes, meaning they take precedence within the function.
  2. Enclosing scope: This scope applies to nested functions. If a variable is not found in the local scope of a nested function, Python looks for it in the enclosing scope, which is the scope of the outer function. This process continues until the variable is found or the global scope is reached.
  3. Global scope: Variables defined at the top level of a module or explicitly declared as global within a function have a global scope. They are accessible throughout the module or the function, respectively. Global variables can be accessed from any part of the code.
  4. Built-in scope: This is the widest scope in Python and includes built-in functions, such as print(), len(), and range(), as well as built-in types like str and list. These entities are always available without the need for import statements.

When accessing a variable, Python searches for it in the local scope first, then in any enclosing scopes, followed by the global scope, and finally in the built-in scope. This search process is known as the “LEGB” rule.

Here’s an example to illustrate Python scope:

python
x = 10  # Global variable

def foo():
    y = 20  # Local variable
    print(x)  # Accessing global variable
    print(y)  # Accessing local variable

foo()
print(x)  # Accessing global variable outside the function

Output:


10
20
10

In this example, x is a global variable accessible within the function foo() as well as outside of it. The variable y is a local variable only accessible within the foo() function.

Leave a Reply

Your email address will not be published. Required fields are marked *

GLOTRU Footer
Popular Features
Popular Services/

Website Development & Design

App Development & Design

Graphic Design

Digital Marketing

SEO (Search Engine Optimization)

SMM (Social Media Marketing)

Cyber Security

Company

GLOTRU Founder & CEO : __Azam

Registared : Trade,MSME,etc

Board of Director

Team

About Us

Contact Us

Privacy Policy

Return & Refund Policy

Abuse Policy

Copyright Policy

Cookie Policy

Terms & Conditions

Universal Terms of Service

Disclaimer

Legal

Sponsorships

Investor

Press Releases

Our Investments

Brands

Newsroom

Business

...

_

Digital Millennium Copyright Act
DMCA.com Protection Status

_

Content similarity detection
Protected by Copyscape

_

***ANTI-PIRACY WARNING***

...................................................................................

Follow Us :

...................................................................................

SECURE SERVER : [Legal] [Privacy Policy] [Universal Terms of Service] [Do not sell my personal information]

SITE HOSTED : GLOTRU SECURE SERVER Asian Data Centre [You can host your site][Click Here]

SSL : Server Type : [Cloudflare] Certificate Issued By : [Let's Encrypt] Signature Algorithm : [ECDSA with SHA-384]

SITE BUILD SOFTWARE : Content Management System (CMS) Softwere

_

_

_