I noticed this while try to create an flask-extension
with pymodm.
Consider a use case of pymodm.MongoModel
.
Models.py (user defined medule)
#line 1
from pymodm import MongoModel, fields,connect
#line 2
connect("mongodb://localhost:27017/project_matrix")
#line 3
class Model(MongoModel):
name = fields.CharField()
#line 4
Model({"name": "test"}).save()
The interesting thing about connect
method of connections.py module is, it uses a module level variable called _CONNECTIONS
to store all the connections.
After importing connect to current name space in #line 2
We are adding a connection to _CONNECTIONS
using connect method.
Then, in #line 4
, we calls save method of TopLevelMongoModel class of models.py module which indirectly calls collections()
of options.py.
options.py imports the method _get_db
of connections.py.
Summery:
Models.py
imports connect method of connections.py to add a connection to module level variable.
options.py imports _get_db of connections.py somehow managed to get a connection from _CONNECTIONS
which is changed by Models.py
.
What is the mechanism/concept behind this? are module level variables are global like in JavaScript
or am I missing something?