English 中文(简体)
如何改变 Python 中的模块级对象? (如 Python 元物体)
原标题:How do I make changes to module-level objects stick in Python? (like Pylons meta objects)

我正在研究一个使用 SQLAlchemy 进行数据库访问的 Python 服务器应用程序。 我想将引擎、会话和元数据对象放在一个元.py 模块中,该模块在整个程序中使用,以访问数据库(如Pyrons Convention)。

引擎和会话对象在元模中初始化为 < code> noone ,然后在其他模块中实际指定真实值。

model/meta.py :

engine = None
Session = None
metadata = Metadata()

model/___init___.py :

from simplesite.model import meta

def init_model():
    # ...
    sm = orm.sessionmaker(...)
    meta.Session = orm.scoped_session(sm)

问题是当我在另一个模块中导入并运行 init_model () , 然后从 mod.py 导入会话时, 它仍然设置为无。 I. e. 。

from model.meta import Session
from model import init_model

init_model()
# Session is still None!

有人能告诉我为什么这是和(或)它是如何在Python应用中工作的吗? 我猜答案对Python是如何工作的更为重要?

关于在整个应用中如何有一个单一的数据库存取点的备选建议(即在大型应用中使用SQLAlchemy的最佳做法)也将受到赞赏。

最佳回答

虽然 @katrielalex在评论中给出了答案,

当您从 model.meta 导入会话 中做 < code> 时,您会创建一个名为 < code> consession 的新的本地变量,该变量与该模块的对象相关。当 < code> init_ model () 运行时,它将 < code> meta 模块中的名称重新组合,但这并不影响您的本地变量 。

init_model() 完成以下工作之前,您可以通过不访问 meta.session 实现自己期望的效果:

from model import meta
from model import init_model
init_model()
Session = meta.Session
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签