Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Superglobal?

Is there is a super global (like PHP) in Python? I have certain variables I want to use throughout my whole project in separate files, classes, and functions, and I don't want to have to keep declaring it throughout each file.

like image 579
Shard Avatar asked Nov 27 '22 06:11

Shard


2 Answers

In theory yes, you can start spewing crud into __builtin__:

>>> import __builtin__
>>> __builtin__.rubbish= 3
>>> rubbish
3

But, don't do this; it's horrible evilness that will give your applications programming-cancer.

classes and functions and i don't want to have to keep declaring

Put them in modules and ‘import’ them when you need to use them.

I have certain variables i want to use throughout my whole project

If you must have unqualified values, just put them in a file called something like “mypackage/constants.py” then:

from mypackage.constants import *

If they really are ‘variables’ in that you change them during app execution, you need to start encapsulating them in objects.

like image 185
bobince Avatar answered Dec 28 '22 01:12

bobince


Create empty superglobal.py module.
In your files do:

import superglobal
superglobal.whatever = loacalWhatever
other = superglobal.other
like image 37
vartec Avatar answered Dec 28 '22 02:12

vartec