Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: "from module import data; print(data)" vs "import module; print(module.data)" Date: Fri, 26 Feb 2016 15:11:51 +1100 Lines: 45 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 X-Trace: news.uni-berlin.de JLXvVuuyBTPYSOJE9HW4YgLkZAF69woO1Nrj84rb3dug== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(especially': 0.07; 'cc:addr:python-list': 0.09; 'collections': 0.09; 'concurrent': 0.09; 'lookup': 0.09; 'modules.': 0.09; 'os.path': 0.09; 'special,': 0.09; 'subject:module': 0.09; 'python': 0.10; 'suggest': 0.15; 'thu,': 0.15; '2016': 0.16; 'cc:name:python': 0.16; 'dotted': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'namespace.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:import': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'attribute': 0.18; 'documented': 0.18; 'module,': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; "aren't": 0.22; 'os,': 0.22; 'feb': 0.23; 'import': 0.24; 'header :In-Reply-To:1': 0.24; 'module': 0.25; "doesn't": 0.26; 'skip:" 20': 0.26; 'fri,': 0.27; 'least': 0.27; 'message- id:@mail.gmail.com': 0.27; 'module.': 0.27; 'package.': 0.27; 'publicly': 0.29; 'another': 0.32; 'aside': 0.32; "d'aprano": 0.33; 'steven': 0.33; 'though.': 0.33; 'received:google.com': 0.35; 'done': 0.35; 'path': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'cases': 0.36; "wasn't": 0.36; 'pm,': 0.36; 'two': 0.37; 'being': 0.37; 'say': 0.37; 'received:209.85.213': 0.37; 'things': 0.38; 'received:209': 0.38; 'subject:from': 0.39; 'your': 0.60; 'matter': 0.63; 'different': 0.63; 'within': 0.64; 'between': 0.65; 'detail.': 0.66; '26,': 0.72; 'special': 0.73; 'analog': 0.84; 'chrisa': 0.84; 'demonstrates': 0.84; 'to:none': 0.91; 'seriously,': 0.91; 'subject:; ': 0.91; 'besides,': 0.93; 'package:': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=12oKuDDEhseFaGZZwfbC2FloXbLEyRdzhVqpEHC1cVw=; b=aPgoAqi+jVJG7vGR79J5DJrAX0DFRDIXlkIlvvcdXTIwx5/qNeX1uNMtO/2LDGpaQ2 9iTG21iYxP/lSdY3eXmLMModFMUqijD2pL0aCRmB6+0VkrWI/lHAS1+1MAIGSwuHxFAW 3EXUwXjn4yr1GWem8q3i9u4cDTw6tIkNiot6Geh2rttM3NnAAsKhHMjpKqvuALoSuF08 1x5ZeBymTu7lwtwm8o2gllHQva3WpepWQI5CanbN77Kvzvl+bS31c1JdDPmgFonjurrK LpDpoINZeAQ+T/V10bKLkSsGk/QrIuxNm8Gc3BtgJxz0OWvzNUL6be0fHZZofB5a71rj 1Qjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=12oKuDDEhseFaGZZwfbC2FloXbLEyRdzhVqpEHC1cVw=; b=F1MW9bZvq3SSXuqpiSsUpWE4RiaJfTMVAmxmcCDdqzSf6lhMB9NwNEwlpDUR823u2F /Fb/fHZjt6uczGbpFVQHCurk2CzSANQIYPaHs/PBVpNemzn2wTS47Ju5JOpfYxme0g4o YMQuu5kWulLyIvg+RcWoYtKllkwkkjZI9IhzfGyeCGFkY5cfPyAMGvp3ZK7ILArn2DMs GNiRFudjglsdm4djIshdkgSNSpbGIyz7mxdyuUgCI+wlGuwmQyJ8i4W4rAMxCBSXSHp0 /hcrroD+kjTriTY7zHO1FTEhpWsa+vmWNm3IZUryN0CC7CWmi/lHXc5yIBJN0cBNyhnV yNsA== X-Gm-Message-State: AD7BkJJB/HAokP8uOxiOUGVXRqd2NMn/orHmB3U2xyTxHTNz+nYQMPqNj066V+6aL0vYCENW4DSESh9i+oVYeQ== X-Received: by 10.51.17.34 with SMTP id gb2mr857066igd.13.1456459911946; Thu, 25 Feb 2016 20:11:51 -0800 (PST) In-Reply-To: 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:103526 On Fri, Feb 26, 2016 at 2:56 PM, Ian Kelly wrote: > On Thu, Feb 25, 2016 at 5:40 PM, Steven D'Aprano wrote: >> If you take "Special cases are not special enough" seriously, you will not >> use `import os.path` since os is not a package: >> >> py> os.__package__ >> '' >> >> and os.path is not part of os, it's just a publicly exposed attribute which >> merely happens to be a module. Being a module doesn't make it special, it's >> just another name in the os namespace. I trust that you wouldn't insist on >> writing: >> >> import os.listdir >> >> (especially since that doesn't work). Neither should you insist on writing >> `import os.path`, since path is documented as a public part of the os >> module. It has done so since at least Python 1.5. > > I disagree. The fact that os is not a package is an implementation > detail. I for one wasn't even aware of it prior to reading your post. > > The name of the concurrent.futures module is "concurrent.futures". If > you want to use it, you import concurrent.futures, not concurrent. > > Likewise, the name of the os.path module is "os.path". If you want to > use it, you import os.path, not os. > > The fact that concurrent and os are two different types of things is irrelevant. > > Besides, packages *are* modules. To take another example, collections > is a package (I checked), and collections.abc is a module. But > collections also contains things that aren't modules. If you want to > use collections.abc, you have to import it, but at the same time you > don't import collections.Counter. This demonstrates that the analog > you suggest between os.path and os.listdir is flawed. Steven is right, though. You can "from module import X" and it doesn't matter whether 'module' is a package or a non-package module; but if you say "import module.X", aside from special cases like os.path, you're asserting that 'module' is a package and 'X' is a module within that package. Yes, a package is a module, but the dotted lookup requires a package. ChrisA