Any variable created on a module level is "exposed" by default.
Hence, a module like this will have three exposed variables:
configpath = '$HOME/.config'
class Configuration(object):
def __init__(self, configpath):
self.configfile = open(configpath, 'rb')
config = Configuration(configpath)
The variables are configpath
, Configuration
and config
. All of these are importable from other modules. You can also access config
s configfile
as config.configfile
.
You can also have configfile accessible globally this way:
configpath = '$HOME/.config'
configfile = None
class Configuration(object):
def __init__(self, configpath):
global configfile
configfile = open(configpath, 'rb')
config = Configuration(configpath)
But there are various tricky problems with this, as if you get a handle on configfile
from another module and it then gets replaced from within Configuration
your original handle will not change. Therefore this only works with mutable objects.
In the above example that means that using configfile
as a global in this way will not be very useful. However, using config
like that could work well.