Reputation: 2682
What is the best way to expose a variable from a module?
import otherDBInterface as odbi
def create(host):
global connection
global cursor
connection = odbi.connect(host)
cursor = connection.cursor()
...
I want to expose the cursor
variable in the module so I can do something like mydb.cursor.execute("select * from foo;")
. I thought using the global
keyword would do this but no such luck. cursor
is an object so I am not sure how I would declare it so that it would be exposed.
Upvotes: 2
Views: 5148
Reputation: 172239
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.
Upvotes: 2
Reputation: 13699
You can wrap your connection information in a class
class Database:
def __init__(self, **kwargs):
if kwargs.get("connection") is not None:
self.connection = kwargs["connection"]
elif kwargs.get("host") is not None:
self.connection = odbi.connect(host)
self.cursor = self.connection.cursor()
mydb = Database(host="localhost")
results = mydb.cursor.execute("select * from foo")
#or use it with a connection
mydb = Database(connection="localhost")
results = mydb.cursor.execute("select * from foo")
Upvotes: 5