在 Python 中布置 MVC 类

发布于 2024-10-14 19:13:56 字数 680 浏览 1 评论 0原文

我正在使用 Python 开发一个项目,并且尝试遵循某种严格的 MVC 模式进行实践。我的想法是将东西分成名为 modelviewcontroller 的包,再加上 Model、<每个的 code>View 和 Controller 类。每个包都会有每个部分的支持文件。

我的问题是我希望能够像这样使用它们:

from controller import Controller

然后在带有控制器类的文件中我可以:

from controller.someclass import SomeClass

但是如果我将它们放在同名的包中,我就会遇到问题。我阅读了有关模块如何工作的信息,并意识到我需要将它们命名为 controller/__init__.pymodel/__init__.pyview/__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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰認得朕 2024-10-21 19:13:56

我在 django 源代码中看到了一些黑魔法,它将类从 base.py 文件提取到 __init__.py 命名空间中。但我不确定这是怎么做到的。 (有关如何执行此操作的信息,请参阅评论。)

据我所知,您可以执行以下两件事之一。

一个-
bar/controller/__init__.py 里面

import os,sys
# Make sure the interpreter knows were your files are.
sys.path.append(os.path.join(os.path.dirname(__file__),'../')
from bar.controller import Controller
from bar.model import Model
from bar.view import View
class Controller(object):
 model = Model()
 view = View()

,现在你制作 bar/model/__init__.pybar/view/__init__.py

B -
bar/controller/__init__.py

class Model(object):
 pass
class View(object):
 pass
class Controller(object):
 model = Model()
 view = View()

编辑:...
阅读您的评论后,我想到了第三种选择。包并不能直接翻译成 python 中的模块。我认为你想要的结果是创建一个像这样的目录结构:

bar/
  __init__.py
  controller.py
  model.py
  view.py

然后在 controller.py 里面

import os,sys
from bar.controller import Controller
from bar.model import Model
from bar.view import View
class Controller(object):
 model = Model()
 view = View()

这对我从 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

import os,sys
# Make sure the interpreter knows were your files are.
sys.path.append(os.path.join(os.path.dirname(__file__),'../')
from bar.controller import Controller
from bar.model import Model
from bar.view import View
class Controller(object):
 model = Model()
 view = View()

And now you make bar/model/__init__.py and bar/view/__init__.py

B -
inside bar/controller/__init__.py

class Model(object):
 pass
class View(object):
 pass
class Controller(object):
 model = Model()
 view = View()

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:

bar/
  __init__.py
  controller.py
  model.py
  view.py

Then inside controller.py

import os,sys
from bar.controller import Controller
from bar.model import Model
from bar.view import View
class Controller(object):
 model = Model()
 view = View()

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))

南笙 2024-10-21 19:13:56

如果我理解正确的话,您在这里感兴趣的就是发生这种情况:

from controller import Controller

没有在 controller/__init__.py 中定义 Controller 类,对吗?

如果是这样,那么只需执行以下操作:

controller/base.py 中:(注意有一个名为 base.py 或其他名称的文件)

class Controller(BaseClass):
  # define Controller here

controller/__init__ 中.py

from base import Controller

现在您可以获得所需的确切语法。

If I understand correctly, all you're interested in doing here is having this happen:

from controller import Controller

without having the Controller class defined in controller/__init__.py is that right?

If so, then just do this:

In controller/base.py: (notice there is a file called base.py or something else)

class Controller(BaseClass):
  # define Controller here

In controller/__init__.py:

from base import Controller

Now you can have the exact syntax you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文