Python Global variables
The scope of a variable in Python refers to the part of the code where the variable can be accessed and used. In Python, there are two types of scope:
- Global scope
- Local scope
Global scope (Global variables):
Variables declared outside of any function or class have global scope and can be accessed from anywhere in the code.
Local scope (Local variables):
Variables declared inside a function or class have local scope and can only be accessed within the function or class where they were declared.
Python Global variables
A global variable is a variable that can be accessed from anywhere in the code, i.e. it has a global scope. Global variables are declared outside of any function or class and can be used by any function in the code. To declare a global variable, you use the "global" keyword before the variable name.
How to create a global variable in Python?
In the above example, the variable x is declared outside of any function and thus has global scope. It can be accessed from anywhere in the code, including from within the my_function function. When the my_function function is called, it prints the value of the global variable x, which is 10.
Python shadowing
Shadowing in Python refers to the situation where a local variable in a function has the same name as a global variable, and thus "shadows" the global variable within the scope of the local variable. When a local variable and a global variable have the same name in Python, the local variable will take precedence over the global variable within the scope of the local variable.
In the above example, you have a global variable named x with a value of 10. Within the my_function function, you declare a local variable named x with a value of 5. When the my_function function is called, it prints the value of the local variable x, which is 5. When you print the value of the global variable x outside of the function, it is still 10.
Python Global keyword
The global keyword in Python is used to indicate that a variable is a global variable, rather than a local variable. It allows you to access and modify the value of a global variable from within a function.
In this example, you have a global variable x with a value of 10. Within the my_function function, you use the global keyword to indicate that you want to access and modify the value of the global variable x, not create a new local variable with the same name. Then you change the value of x to 5. When you call the my_function function, it prints the new value of x, which is 5. When you print the value of x outside of the function, it is also 5, because the global variable has been modified.
Python Local Variable
A local variable is a variable declared inside a function or class. Local variables have a local scope, which means that they can only be accessed and used within the function or class where they were declared.
In the above example, the variable x is declared as a local variable within the my_function function with a value of 5. When the my_function function is called, it prints the value of the local variable x, which is 5. However, if you try to access x outside of the function, a NameError will be raised because x is not defined in the global scope. On the other hand, global variables can be accessed from anywhere in the code. However, it is generally considered best practice to avoid using global variables as much as possible, as they can make the code harder to understand and maintain.
Python variable's lifetime
It's important to keep in mind that local variables are only accessible within their scope, and are destroyed once the function or class is finished executing. This is known as the variable's lifetime. When a function is called again, a new instance of the local variable is created and its value is not remembered from the previous call.
Conclusion
By contrast, global variables are accessible from anywhere in the code, and their lifetime extends from the time they are defined until the end of the program. However, as mentioned earlier, it is generally considered best practice to avoid using global variables as much as possible, as they can make the code harder to understand and maintain.
- Python Interview Questions (Part 2)
- Python Interview Questions (Part 3)
- What is python used for?
- Is Python interpreted, or compiled, or both?
- Explain how python is interpreted
- How do I install pip on Windows?
- How do you protect Python source code?
- What are the disadvantages of the Python?
- How would you achieve web scraping in Python?
- How to Python Script executable on Unix
- What is the difference between .py and .pyc files?
- What is __init__.py used for in Python?
- What does __name__=='__main__' in Python mean?
- What is docstring in Python?
- What is the difference between runtime and compile time?
- How to use *args and **kwargs in Python
- Purpose of "/" and "//" operator in python?
- What is the purpose pass statement in python?
- Why isn't there a switch or case statement in Python?
- How does the ternary operator work in Python?
- What is the purpose of "self" in Python
- How do you debug a program in Python?
- What are literals in python?
- Is Python call-by-value or call-by-reference?
- What is the process of compilation and Loading in python?
- Static analysis tools in Python
- What does the 'yield' keyword do in Python?
- Python Not Equal Operator (!=)
- What is the difference between 'is' and '==' in python
- What is the difference between = and == in Python?
- How are the functions help() and dir() different?
- What is the python keyword "with" used for?
- Why isn't all memory freed when CPython exits
- Difference between Mutable and Immutable in Python
- Python Split Regex: How to use re.split() function?
- Accessor and Mutator methods in Python
- How to Implement an 'enum' in Python
- What is Object in Python?
- How to determine the type of instance and inheritance in Python
- Python Inheritance
- How is Inheritance and Overriding methods are related?
- How can you create a copy of an object in Python?
- Class Attributes vs Instance Attributes in Python
- Static class variables in Python
- Difference between @staticmethod and @classmethod in Python
- How to Get a List of Class Attributes in Python
- Does Python supports interfaces like in Java or C#?
- How To Work with Unicode strings in Python
- Difference between lists and tuples in Python?
- What are differences between List and Dictionary in Python
- Different file processing modes supported by Python
- Python append to a file
- Difference Between Multithreading vs Multiprocessing in Python
- Is there any way to kill a Thread in Python?
- What is the use of lambda in Python?
- What is map, filter and reduce in python?
- Is monkey patching considered good programming practice?
- What is "typeerror: 'module' object is not callable"
- Python: TypeError: unhashable type: 'list'
- How to convert bytes to string in Python?
- What are metaclasses in Python?