Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Dan Stromberg Newsgroups: comp.lang.python Subject: Re: "from module import data; print(data)" vs "import module; print(module.data)" Date: Thu, 25 Feb 2016 08:20:59 -0800 Lines: 42 Message-ID: References: <56ce7762$0$11100$c3e8da3@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de YeyZnaIhSAUNzMNL2wBMOQhjvYKSz3leU4JXw1a2Z7sg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'see.': 0.07; 'cc:addr :python-list': 0.09; 'imports': 0.09; 'mutable': 0.09; 'subject:module': 0.09; 'wed,': 0.15; 'applies': 0.15; 'importing': 0.15; 'thu,': 0.15; '12:07,': 0.16; '2016': 0.16; '24,': 0.16; 'cases:': 0.16; 'cc:name:python list': 0.16; 'intuition': 0.16; 'opposite': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:import': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; 'thanks.': 0.18; '>>>': 0.20; '(not': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(a)': 0.22; 'affects': 0.22; 'class,': 0.22; 'am,': 0.23; 'feb': 0.23; 'import': 0.24; 'patch': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'compare': 0.27; 'message-id:@mail.gmail.com': 0.27; 'module.': 0.27; 'dan': 0.29; 'testing.': 0.29; 'regardless': 0.31; "can't": 0.32; "d'aprano": 0.33; 'steven': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'instance': 0.35; 'unit': 0.35; 'there': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'pm,': 0.36; 'two': 0.37; 'say': 0.37; 'doing': 0.38; 'no,': 0.38; 'received:209': 0.38; '(1)': 0.38; 'anything': 0.38; 'shared': 0.38; 'data': 0.39; 'subject:from': 0.39; 'area': 0.39; 'received:209.85.214': 0.39; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'telling': 0.61; 'is.': 0.63; 'else.': 0.66; 'thursday': 0.66; 'subject': 0.70; 'accessed.': 0.84; 'surface': 0.84; 'subject:; ': 0.91; 'increases': 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:to :cc:content-type; bh=TmRN/2bV50YBgMA3EZCdthakdKXBJbmbcciEu+BukcU=; b=dcA7MFUFZL/XWa2WPH865XjIuVGrt2olmqx359HcjA86M0/vJOj7jHauvObcWtkWhb EYkhtla98GgzOsLuVr4u6TfD7ZGT8w1HJJdoE4H6vXlUvVdoT23leO3dryWI90VV8zC8 x0pWBoHt00slrHJadsI999dI90DA7HvDHSrJXQ/6NDa4Tz6DqQa68FWD3EMJjUjbISn3 TZ/zFn/gUtIXIabmQ3l8ALPfhbzMh2BjMM0QNjQsKddvzX1N0i8v/4bf/yBxmKIrAsyB uSEyC67Q05sae8VPR5b4dzWC7Dnr/2KWJwH/TqD9fp1wQRMXuNElMEbp8vhwTbwBMDDX 0WuA== 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:to:cc:content-type; bh=TmRN/2bV50YBgMA3EZCdthakdKXBJbmbcciEu+BukcU=; b=jVxu+2Jduthf1pBCoDVx+BkX/vIhYbz3YAnRsQr1jbv43KNa3EeqNt2dgYH44y0Guw 83tRwR/A27HVMy30LsrEmaBgZofTbD/5SmmqjW/mO63PT7TGnrdR0FnlwEFp4nJNP1ea G+ebUioyjIIHwQX5oMdB2M898UW54jnXstDS62rud9BcD/Gmeb+wuFwcGd7TGrcsJyNf stoysfigpXGkevdeEwsC4gSsphmOesmrNExRO4RhbpNpnRcucpb1PZTpPamD4FKdqsBZ vT+16UEnWtrgCQlGwVR/0jmo/CRC4L6E/6RvccfUZH3ixMwLqkJoLdB3WVmePGwJDmSK GTqQ== X-Gm-Message-State: AG10YOQ2B55SvzA7Kf8guG0bu2g0aoOhgqooYjqWtGVMYE9F1AYgwT8cqQefeGXZKPBFd0DVZF8je9hBepzrnA== X-Received: by 10.182.55.10 with SMTP id n10mr37263661obp.68.1456417259395; Thu, 25 Feb 2016 08:20:59 -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:103505 On Thu, Feb 25, 2016 at 8:15 AM, Dan Stromberg wrote: > On Wed, Feb 24, 2016 at 7:39 PM, Steven D'Aprano > 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. > > Please ignore: 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. Correction! My intuition is telling me that "module.data; module.data.mutate()" would be easier to monkey patch in a way that all modules will see. Is that fair to say?