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


Groups > comp.lang.python > #103504

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

From Dan Stromberg <drsalists@gmail.com>
Newsgroups comp.lang.python
Subject Re: "from module import data; print(data)" vs "import module; print(module.data)"
Date 2016-02-25 08:15 -0800
Message-ID <mailman.128.1456416920.20994.python-list@python.org> (permalink)
References <mailman.111.1456362461.20994.python-list@python.org> <56ce7762$0$11100$c3e8da3@news.astraweb.com>

Show all headers | View raw


On Wed, Feb 24, 2016 at 7:39 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Thursday 25 February 2016 12:07, Dan Stromberg wrote:
>
>> Could people please compare and contrast the two ways of doing imports
>> in the Subject line?
>
> from module import data; print(data)
>
> import module; print(module.data)

>> Is it fair to say that the former increases the surface area of your
>> shared (sometimes mutable) state?
>
> No, I can't say that it is. If anything, the opposite is the case: it
> decreases the surface area, because `data` now is local to the importing
> (not imported) module. Rebinding data will not affect anything else.

> There are two scenarios (`from module import...` versus `import ...`), each
> with two cases: mutation, and rebinding:
>
> (1) from module import data
>
> (a) Mutation (only applies to mutable data like lists, dicts etc)
>
> In place mutation affects everything relying on module.data regardless of
> how or where it is accessed.

We have some scenarios where "data" is an instance of a class, and we
need to monkey patch it for unit testing.

My intuition is telling me that "module.data; data.mutate()" would be
easier to monkey patch in a way that all modules will see.  Is that
fair to say?

Thanks.

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