在 Python 中,仅导入某些内容以便以更方便的方式公开它是否被认为是不好的做法?
如果没有例子,这可能没有意义。我在 Django 应用程序中使用 python-disqus ,并且将其包装以更好地解耦它来自我的应用程序的其余部分。
我有一个文件 disqus.py,它导入 disqusapi 并定义了许多辅助函数。在应用程序代码的其他地方,我可以简单地添加 from mango import disqus
,但在一些地方,有必要捕获在进行 API 调用时可能引发的异常。这意味着我被迫做类似的事情:
from disqusapi import APIError
from mango import disqus
try:
disqus.thread.fetch(1)
except APIError, error:
logger.warn('Disqus API error: %s' % error)
如果我要在 disqus.py 顶部导入 APIError
,我可以改为写:
from mango import disqus
try:
disqus.thread.fetch(1)
except disqus.APIError, error:
logger.warn('Disqus API error: %s' % error)
Pyflakes 抱怨未使用的导入,但这看起来像合理的事情。我应该高兴地忽略这里的 Pyflakes,还是我错过了更好的选择?
需要明确的是,我不需要以任何方式修改 disqusapi.APIError
,因此不需要子类化。
This probably won't make sense without an example. I'm using python-disqus in my Django app, and I'm wrapping it to better decouple it from the rest of my application.
I have a file, disqus.py, which imports disqusapi
and defines a number of helper functions. Elsewhere in the application code I can simply add from mango import disqus
, but in a few places it's necessary to capture exceptions which may be raised when making API calls. This means that I'm forced to do something like:
from disqusapi import APIError
from mango import disqus
try:
disqus.thread.fetch(1)
except APIError, error:
logger.warn('Disqus API error: %s' % error)
If instead I were to import APIError
at the top of disqus.py, I could instead write:
from mango import disqus
try:
disqus.thread.fetch(1)
except disqus.APIError, error:
logger.warn('Disqus API error: %s' % error)
Pyflakes complains about the unused import, but this seems like a reasonable thing to do. Should I happily ignore Pyflakes here, or am I missing a better option?
To be clear, I don't need to modify disqusapi.APIError
in any way, so subclassing is unnecessary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短的回答:不。
长的回答,如果您正在编写一个包装器来使自己的代码更简单、更容易理解,那么您可以做任何您喜欢的事情。如果您想包装其他代码,或导入模块并对其进行子类化,或导入模块并更改其方法。
如果您正在为其他人编写代码,您可能需要添加注释来解释您正在做的事情。如果只是为了自己,那一切都好。
Short answer: No.
Long answer, if you are writing a wrapper to make your own code simpler and easier for you to understand you can do whatever you like. If you want to wrap other code, or import modules and subclass them, or import modules, and change their methods.
If you're writing code for other people, you probably want to add comments to explain what you're doing. If it's just for yourself, it's all good.
__init__.py
或包充满明显“未使用”的导入是相当常见的。我认为 pyflakes 未使用的想法似乎有点简单化,因为可以清楚地看到这个导入被使用了 - 只是在另一个模块中。如果你不得不忽略它,那就很烦人了。也许 pyflakes 有某种编译指示可以覆盖这种行为。
它的风格是否良好实际上取决于应用程序的结构。我认为你可以为双方辩护
It's fairly common for an
__init__.py
or a package to be full of apparently "unused" imports. I think that pyflakes idea of unused seems a little simplistic, as it can be plainly seen that this import is used - just in another module.It's annoying if you have to just ignore it. Perhaps there is some sort of pragma for pyflakes to override this behaviour.
Whether or not it's good style really depends on the structure of the application. I think you can make arguments for both sides
如果您需要使用某些东西并且它不会在您的命名空间中发生冲突,那么
from x import y
它并不是不合适的。但是,出于自我记录的目的,您可能更喜欢第二种选择。如果有人正在阅读您的代码,他们可能不知道错误与哪个 API 相关,但从名称空间中提取它可以避免任何歧义。
If you need to use something and it doesn't collide in your namespace it is not inappropriate to
from x import y
it.However, you may prefer the second option for self-documentation sake. If someone were reading your code, they may not know which API the error is related to, but pulling it from a namespace avoids any ambiguity.