在 Python 中布置 MVC 类
我正在使用 Python 开发一个项目,并且尝试遵循某种严格的 MVC 模式进行实践。我的想法是将东西分成名为 model
、view
和 controller
的包,再加上 Model
、<每个的 code>View 和 Controller
类。每个包都会有每个部分的支持文件。
我的问题是我希望能够像这样使用它们:
from controller import Controller
然后在带有控制器类的文件中我可以:
from controller.someclass import SomeClass
但是如果我将它们放在同名的包中,我就会遇到问题。我阅读了有关模块如何工作的信息,并意识到我需要将它们命名为 controller/__init__.py
、model/__init__.py
和 view/__init__.py< /code>,但将它们放在该文件中似乎很奇怪,而且它们都在 gedit 中显示为
__init__.py
,这有点烦人,
有没有更好的方法可以做到这一点?我以正确的方式处理这件事吗?
I'm working on a project in Python, and I'm trying to follow a somewhat-strict MVC pattern for practice. My idea was to separate things out into packages named model
, view
and controller
, plus have a Model
, View
and Controller
class for each. Each package would have the supporting files for each piece.
My problem is that I want to be able to use them like:
from controller import Controller
And then in the file with the Controller class I can:
from controller.someclass import SomeClass
But if I put them in packages with the same name, I get problems. I read up about how modules work, and realized I needed to name them controller/__init__.py
, model/__init__.py
and view/__init__.py
, but it seems weird to put them in that file, and it's kind of annoying that all of them show up gedit as __init__.py
Is there any better way of doing this? Am I going about this the right way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 django 源代码中看到了一些黑魔法,它将类从
base.py
文件提取到__init__.py
命名空间中。但我不确定这是怎么做到的。 (有关如何执行此操作的信息,请参阅评论。)据我所知,您可以执行以下两件事之一。
一个-
在
bar/controller/__init__.py
里面,现在你制作
bar/model/__init__.py
和bar/view/__init__.py
B -
在
bar/controller/__init__.py
内编辑:...
阅读您的评论后,我想到了第三种选择。包并不能直接翻译成 python 中的模块。我认为你想要的结果是创建一个像这样的目录结构:
然后在
controller.py
里面这对我从 java 来是一个巨大的障碍。您的类文件名不必与类名匹配。将它们视为一个步骤,您进入文件夹(模块),然后进入文件(.py),然后导入您的类。(模型(对象))
I've seen some black magic in the django source that pulls classes from a
base.py
file into the__init__.py
namespace. However I'm not sure how that's done. ( See comments for how to do this. )From what I do know, you can do one of two things.
A -
inside
bar/controller/__init__.py
And now you make
bar/model/__init__.py
andbar/view/__init__.py
B -
inside
bar/controller/__init__.py
Edit:...
After reading your comment, a third option comes to mind. A package doesn't litertly translate into a module in python. I think your desired result is to create a directory structure like this:
Then inside
controller.py
This was a huge hurdle for me to get coming from java. Your class file names do not have to match the class name. Think of them as a step, you step into the folder(module) and then into the file(.py) and then you import your class.(Model(object))
如果我理解正确的话,您在这里感兴趣的就是发生这种情况:
没有在
controller/__init__.py
中定义Controller
类,对吗?如果是这样,那么只需执行以下操作:
在
controller/base.py
中:(注意有一个名为base.py
或其他名称的文件)在
controller/__init__ 中.py
:现在您可以获得所需的确切语法。
If I understand correctly, all you're interested in doing here is having this happen:
without having the
Controller
class defined incontroller/__init__.py
is that right?If so, then just do this:
In
controller/base.py
: (notice there is a file calledbase.py
or something else)In
controller/__init__.py
:Now you can have the exact syntax you are looking for.