Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andre Guerra
2,799 PointsIs it a good practice to set variables outside def's and classes in a module to use them as global by using import?
I suppose this behaviour would generate something like a static variable type from Java, where your variable exists and is set even without a specific instance of a class. Appreciate any insights and/or comments. Cheers!
Example:
in someCustomModule.py
someVar = "hey I get assigned at import"
def doSomething(): ...
in myExecutedModule.py
import someCustomModule.py
....
print(someCustomModule(someVar))
...
Terminal
$ python myExecutedModule.py
$ hey I get assigned at import
1 Answer
Kent Åsvang
18,823 PointsIf it makes sense for the modularity then I suppose it's okay, but I struggle with finding an example that would benefit from this. In most cases I can think of you would be better of using json or other regular textfiles.