Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103526

Re: "from module import data; print(data)" vs "import module; print(module.data)"

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: "from module import data; print(data)" vs "import module; print(module.data)"
Date 2016-02-26 15:11 +1100
Message-ID <mailman.140.1456459915.20994.python-list@python.org> (permalink)
References (1 earlier) <535b56b5-f836-4cd6-ae95-33f18004c90b@googlegroups.com> <dj9d10F4gciU1@mid.individual.net> <mailman.138.1456443525.20994.python-list@python.org> <56cf9f01$0$1620$c3e8da3$5496439d@news.astraweb.com> <CALwzidkYZDwwxfA1tmZo4FCt173N+BD0wcwHtQLnE+CVOJSwUg@mail.gmail.com>

Show all headers | View raw


On Fri, Feb 26, 2016 at 2:56 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Thu, Feb 25, 2016 at 5:40 PM, Steven D'Aprano <steve@pearwood.info> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

"from module import data; print(data)" vs "import module; print(module.data)" Dan Stromberg <drsalists@gmail.com> - 2016-02-24 17:07 -0800
  Re: "from module import data; print(data)" vs "import module; print(module.data)" Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-02-25 14:39 +1100
    Re: "from module import data; print(data)" vs "import module; print(module.data)" Dan Stromberg <drsalists@gmail.com> - 2016-02-25 08:15 -0800
    Re: "from module import data; print(data)" vs "import module; print(module.data)" Dan Stromberg <drsalists@gmail.com> - 2016-02-25 08:20 -0800
    Re: "from module import data; print(data)" vs "import module; print(module.data)" Ethan Furman <ethan@stoneleaf.us> - 2016-02-25 08:35 -0800
    Re: "from module import data; print(data)" vs "import module; print(module.data)" Ethan Furman <ethan@stoneleaf.us> - 2016-02-25 08:51 -0800
  Re: "from module import data; print(data)" vs "import module; print(module.data)" sohcahtoa82@gmail.com - 2016-02-25 12:00 -0800
    Re: "from module import data; print(data)" vs "import module; print(module.data)" Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-02-26 11:20 +1300
      Re: "from module import data; print(data)" vs "import module; print(module.data)" Ben Finney <ben+python@benfinney.id.au> - 2016-02-26 10:38 +1100
        Re: "from module import data; print(data)" vs "import module; print(module.data)" Steven D'Aprano <steve@pearwood.info> - 2016-02-26 11:40 +1100
          Re: "from module import data; print(data)" vs "import module; print(module.data)" Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-25 20:56 -0700
          Re: "from module import data; print(data)" vs "import module; print(module.data)" Chris Angelico <rosuav@gmail.com> - 2016-02-26 15:11 +1100
          Re: "from module import data; print(data)" vs "import module; print(module.data)" Ben Finney <ben+python@benfinney.id.au> - 2016-02-26 15:37 +1100

csiph-web