[Python-Dev] Relative import

Guido van Rossum guido at python.org
Tue Dec 16 13:26:59 EST 2003


A lot people have presented a good case for relative imports.  Nobody
has argued to keep the status quo (where imports are ambiguous about
whether they are meant to be absolute or relative).  So I suggest that
in 2.4, we introduce the leading dot notation for relative import,
while still allowing relative import without a leading dot.  In
2.5 we can start warning about relative import without a leading dot
(although that will undoubtedly get complaints from folks who have
code that needs to work with 2.3 still).  In 3.0 we can retire
ambiguous import.

The use case for multiple dots should be obvious: inside a highly
structured package, modules inside one subpackage must have a way to
do relative import from another subpackage of the same parent package.

There is the remaining issue of what exactly the syntax would be.  I
propose to extend the from clause to allow one or more dots before the
dotted name, and to make the dotted name optional if at least one
leading dot is found.  I propose not to change from-less import.
Examples:

  from .foo import bar
  from .foo.bar import xxx
  from . import foobar as barfoo
  from ..foo.bar import *
  from ... import foobar, barfoo

Grammar change (see Grammar/Grammar):

dotted_name: NAME ('.' NAME)* | '.'+ [NAME ('.' NAME)*]

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list
::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com



自怼圈/年番新

DU21.7
关于 ~ DebugUself with DAMA ;-)


关注公众号, 持续获得相关各种嗯哼:
zoomquiet


粤ICP备18025058号-1
公安备案号: 44049002000656 ...::