在Python中非常专门地导入始终是个好主意吗?

编程入门 行业动态 更新时间:2024-10-11 21:31:16
本文介绍了在Python中非常专门地导入始终是个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这几乎是Python,但要向Django用户询问.

This is pretty much Python, but asking from a Django user.

假设这是Django应用程序的布局方式:

Suppose this is how Django apps are layout:

Webclient

Webclient

  • 应用
    • myapp#1
      • 图书馆
        • library.py
        • views.py

        如果我正在使用views.py,并且我想导入library.py,哪一个看起来更好?

        If I am working with views.py, and I want to import library.py, which one seems better?

        from webclient.apps.myapp.library import LibraryClass from webclient.apps.myapp.library.library import LibraryClass

        我正在使用PyCharm,无论哪种方式都不会抱怨未解决的引用".特别指定导入是否更好.如果可能的话,第二种导入方法是否更有可能避免名称冲突(例如/library/有多个.py文件)?

        I am using PyCharm, and either way doesn't complain about "unresolved references". Is it better to import very speifically. Is second import method more likely to avoid name collison, if possible at all (say /library/ has several .py files)?

        谢谢.

        推荐答案

        您应始终从定义名称的位置导入名称.这样,如果 webclient.apps.myapp.library 有一天应该停止导入 LibraryClass ,那么您就不会中断其他导入.

        You should always import names from where they're defined. That way if webclient.apps.myapp.library should stop importing LibraryClass one day, you won't break the other imports.

更多推荐

在Python中非常专门地导入始终是个好主意吗?

本文发布于:2023-10-30 04:37:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541791.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是个   好主意   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!