- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain the visibility of global variables in imported modules in Python?
Globals in Python are global to a module, not across all modules. (Unlike C, where a global is the same across all implementation files unless you explicitly make it static.). If you need truly global variables from imported modules, you can set those at an attribute of the module where you're importing it.
import module1 module1.a=3
On the other hand, if a is shared by a whole lot of modules, put it somewhere else, and have everyone import it:
global_module.py module1.py: import global_module def fun(): print global_module.var Other files: import global_module import module1 global_module.var = 3 module1.fun()
- Related Articles
- How do I share global variables across modules in Python?
- How to disable logging from imported modules in Python?
- Global and Local Variables in Python?
- Global vs Local variables in Python
- How to find which Python modules are being imported from a package?
- Global variables in Java
- What is the difference between global and local variables in Python?
- What are the rules for local and global variables in Python?
- Global Scope Variables in Postman?
- Global Variables in Lua Programming
- What are local variables and global variables in C++?
- Initialization of global and static variables in C
- Global and Local Variables in Java
- What are global variables in C++?
- Global and Local Variables in C#

Advertisements