Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: "from module import data; print(data)" vs "import module; print(module.data)" Date: Fri, 26 Feb 2016 15:37:14 +1100 Lines: 79 Message-ID: References: <535b56b5-f836-4cd6-ae95-33f18004c90b@googlegroups.com> <56cf9f01$0$1620$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de iqcIUyAa8vYcVafkWBmovw6UHC7n3aVZ5/20uRiNFfHA== Cancel-Lock: sha1:VKcyX/RQz7h3VCEEDhweoaMPLaY= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '8bit%:30': 0.09; 'attribute.': 0.09; 'imports': 0.09; 'normally,': 0.09; 'os.path': 0.09; 'python:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'violates': 0.09; 'python': 0.10; ':-)': 0.12; 'package,': 0.13; 'appropriate': 0.14; 'importing': 0.15; '2016': 0.16; '`path`': 0.16; 'attributes.': 0.16; 'behaviour.': 0.16; 'enough.': 0.16; 'exported': 0.16; 'gregory': 0.16; 'imo.': 0.16; 'namespace.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:import': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'documented': 0.18; '>>>': 0.20; '"",': 0.22; "aren't": 0.22; 'os,': 0.22; 'am,': 0.23; 'feb': 0.23; 'import': 0.24; '(most': 0.24; 'module': 0.25; "i've": 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'fri,': 0.27; 'logging': 0.27; 'least': 0.27; 'module.': 0.27; 'function': 0.28; 'behaviour': 0.29; 'cases.': 0.29; 'itself,': 0.29; 'publicly': 0.29; 'another': 0.32; 'noticed': 0.32; "d'aprano": 0.33; 'rule': 0.33; 'steven': 0.33; 'traceback': 0.33; 'file': 0.34; 'except': 0.34; 'path': 0.35; 'primarily': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'cases': 0.36; 'to:addr:python- list': 0.36; 'received:org': 0.37; 'available.': 0.37; 'seem': 0.37; 'difference': 0.38; 'names': 0.38; 'goes': 0.39; 'subject:from': 0.39; 'enough': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'space': 0.40; 'some': 0.40; 'your': 0.60; 'email addr:gmail.com': 0.62; 'making': 0.62; 'qualified': 0.63; 'between': 0.65; 'talking': 0.67; 'skip:\xe2 10': 0.70; 'special': 0.73; '8bit%:46': 0.76; '_o__)': 0.84; 'depended': 0.84; 'received:125': 0.84; 'zen': 0.84; '8bit%:33': 0.91; 'seriously,': 0.91; 'subject:; ': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103527 Steven D'Aprano writes: > On Fri, 26 Feb 2016 10:38 am, Ben Finney wrote: > > > Gregory Ewing writes: > > > >> sohcahtoa82@gmail.com wrote: > >> > Now, I've noticed people talking about importing os.path. Is there any > >> > reason to use "import os.path" rather than "import os"? Both of them > >> > will still put the "os" module into the global namespace. > >> > >> In the case of os.path it doesn't matter, because the > >> os module imports the appropriate path module automatically. > > > > My position is that behaviour violates one of the principles of the Zen > > of Python: “Special cases aren't special enough to break the rules.” > > But it's not special. It's the standard behaviour of any module which offers > a public name That is the special behaviour. ‘os’ is a package that has a sub-module ‘path’. Normally, to get at a module inside a package, you import it with a qualified name:: import os.path The special case is that ‘os’ also wants the ‘path’ module itself, and so the name happens to be available as a module attribute. That's a special case that can not be depended on for other cases. > `import os` makes all the names in the os name space available. Since ‘os.path’ is a sub-module of the ‘os’ package, it should not also be exported from the ‘os’ module attributes. The special case is confusing. > There's no difference between (say) `os.listdir` and `os.path` except > that listdir happens to be a function and path happens to be a module. There's no difference between ‘logging.info’ and ‘logging.config’ except that ‘info’ happens to be a function and ‘config’ happens to be a module. The difference is salient:: >>> import logging >>> logging.info >>> logging.config Traceback (most recent call last): File "", line 1, in AttributeError: module 'logging' has no attribute 'config' That's the case normally, when one name is primarily an attribute and another name is primarily a sub-module. The ‘os.path’ case is not special enough to break that rule IMO. > `path` has been a documented public attribute of the `os` module since at > least Python 1.5: Then it's been violating that principle for that long :-) > If you take "Special cases are not special enough" seriously, you will > not use `import os.path` since os is not a package The implementation is special. That doesn't exempt it from the principle that a special case isn't special enough to break expectations. > and os.path is not part of os, it's just a publicly exposed attribute > which merely happens to be a module. You seem to be making my case for me: ‘os’ is indeed special. It goes to some legth to hide that specialness; I think it doesn't go far enough. -- \ “If you don't want your beliefs to be ridiculed, don't have | `\ such ridiculous beliefs.” —Greta Christina, 2011-10-22 | _o__) | Ben Finney