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


Groups > comp.lang.python > #103523

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: "from module import data; print(data)" vs "import module; print(module.data)"
Date Fri, 26 Feb 2016 10:38:36 +1100
Lines 30
Message-ID <mailman.138.1456443525.20994.python-list@python.org> (permalink)
References <mailman.111.1456362461.20994.python-list@python.org> <535b56b5-f836-4cd6-ae95-33f18004c90b@googlegroups.com> <dj9d10F4gciU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de MqGzmb12Qe8QtQir0XpSsgDTm4AQg8fNni90SIJLzHXQ==
Cancel-Lock sha1://+WkaI/Ltex6lHe047o+azz88w=
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'imports': 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; 'appropriate': 0.14; 'explicitly': 0.15; 'importing': 0.15; 'gregory': 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; "aren't": 0.22; 'import': 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; 'behaviour': 0.29; 'becomes': 0.30; 'normally': 0.30; 'noticed': 0.32; 'case,': 0.34; 'path': 0.35; 'sometimes': 0.35; 'but': 0.36; 'there': 0.36; 'cases': 0.36; 'to:addr:python-list': 0.36; 'received:org': 0.37; "won't": 0.38; 'subject:from': 0.39; 'enough': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'email addr:gmail.com': 0.62; 'necessarily': 0.63; 'talking': 0.67; 'choose': 0.68; 'skip:\xe2 10': 0.70; '8bit%:43': 0.72; 'special': 0.73; '_o__)': 0.84; 'received:125': 0.84; 'zen': 0.84; 'subject:; ': 0.91; 'louis': 0.93
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 <bignose+hates-spam@benfinney.id.au>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103523

Show key headers only | View raw


Gregory Ewing <greg.ewing@canterbury.ac.nz> 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 in general, importing a package won't necessarily
> import submodules under it, so sometimes you need to
> import somepackage.somemodule explicitly.

Because that's normally the case, I choose not to rely on that special
behaviour of ‘os’. If I need ‘os.path’, I import it explicitly so no
reader needs to guess::

    import os
    import os.path

-- 
 \       “Crime is contagious… if the government becomes a lawbreaker, |
  `\          it breeds contempt for the law.” —Justice Louis Brandeis |
_o__)                                                                  |
Ben Finney

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