Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

What is a scope in python?

User image

Published by

sanya sanya

Published at: 21st May, 2023
2.545 mins read

A scope in Python is a section of the code where a specific identifier, such a variable or function, is declared and accessible. An identifier's use depends on where it appears in the programme and how it is defined. The two primary scope types in Python are global and local.

Global Scope

A global scope in Python is a scope that includes the whole application. A global identifier is one that is specified outside of a function or class and is accessible throughout the whole programme. All functions and classes in the programme have access to variables, functions, and classes specified in the global scope.

In Python, you merely declare a global variable outside of any function or class. For illustration, the code that follows creates the global variable x:

https://images.codingblocks.com/data-science/global_scope_1.png

Once defined, this variable is accessible throughout the whole program, including within functions and classes.

https://images.codingblocks.com/data-science/global_scope_2.png

Local Scope

A local scope in Python is a scope that is specified within a function or class. Any identifier defined inside of a class or function is regarded as local and can only be accessed inside of that class or function. Local variables are removed when the function or class is terminated and have a short lifespan.

In Python, you use the 'def' keyword to declare a local variable inside of a function or class. As an illustration, the code that follows creates a local variable called y inside of a function:

https://images.codingblocks.com/data-science/local_scope_1.png

Once defined, this variable can only be used inside the function. A 'NameError' will appear if you try to access the variable outside of the function.

https://images.codingblocks.com/data-science/local_scope_2.png

Nested Scopes

A local scope can be defined inside of another local scope in Python. An instance of a nested scope is this. A nested scope and all of its child scopes are the only places where an identifier declared inside of that scope can be accessed.

For instance, the code below creates a function containing two nested functions, each having a local variable of its own:

https://images.codingblocks.com/data-science/nested_scope.png

In this illustration, the x variable is declared in the local scope of the outer function and is available to both the inner and outside functions. The local scopes of the inner functions that define the variables y and z are the only places from which they can be accessed.

Built-in Scope

A built-in scope in Python also has a list of predefined identifiers that are accessible to all Python programmes. These built-in identifiers include classes like 'list', 'dict', and 'set' as well as functions like 'print()', 'input()', and 'len()'.

In Python, you can access a built-in identifier by calling it by name in your programme. For instance, you can utilize the print() method by calling it with the appropriate arguments:

https://images.codingblocks.com/data-science/built-in_scope.png

Conclusion

A scope in Python is a section of the programme where a certain identifier is declared and accessible. The two primary scope types in Python are global and local. An identifier's use depends on where it appears in the programme and how it is defined.

Library

WEB DEVELOPMENT

FAANG QUESTIONS

On this page

Global Scope

Local Scope

Nested Scopes

Built-in Scope

Conclusion