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:15:16 -0800 Lines: 36 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 gd2mZowEai03fB+074ierAG9xUyDEfw9lcF92ZbsvvNQ== 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; 'received:209.85.218': 0.10; 'wed,': 0.15; 'applies': 0.15; 'importing': 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; '(not': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(a)': 0.22; 'affects': 0.22; 'class,': 0.22; '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; '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=8TC/VnnmShzWhqyWNKiGqbc3OKJuLqoEYsiw6nrzQEo=; b=pUwUIU0RO39896W0TYmCviGGyLMpSnGOmXSdpl3pLSUCvj0+P8F0HF1jrJFrT4bg3O O4k3DQ6sxlx3EAP2ATc5IK3Nxu8BelQmduZe0ktWlZmWszG7ndJRucOKFy2W9oghsNMh fyOkrNepkCFUFFSD/mpi8aw6IPaOVJAE/euojPAcXH9Up1k2utS9onfPcJRHhZysX9+J a29+Nm0M7+Zv4PODgF0FNgunOXzZNRNeVHE4P5nyx4x+T+KDywgNc41VILsnyBPAFBGe J5nIsWt+7fKMbXjlY32SnnygIET4kvwqXChlBiRAsZxYFoS3fNMaQaJFHgD757VUqsw1 3bEA== 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=8TC/VnnmShzWhqyWNKiGqbc3OKJuLqoEYsiw6nrzQEo=; b=b0LdRiwy70KWOEpd6zZaIhbDsh3xIfJBhCO0aYdE/Fj0M9U0zZhOxd+UiWmpdYFT+M mJqHQVeLNIjIHi3x7qvojqLP8QhLKv1ecdGk0Dm3XuyOIOU3nFs/rsatTknnyXvD9bAk zayzN727pB1bEEJfsJyWasVLfUBXZIO6RdgYT7Uc8QeNL+Vc9K+gs3xlbdkO7TrSAQWr e6XQlS+SejsTToXfALYWp14MUyptKn/8ccPfc1XzyKhQbVkWRMwqTPoJ0hOevAz/Moy8 i7mePiW4FqO/cK1n7tYcLbYP6CikuUEuGVH/95KKka0u5S2uBidAOOgjhHTzn7/Ibgi3 +YPw== X-Gm-Message-State: AG10YOQMEdxIEQP0jZlk30dAVvzRo+OV5JNcsb0JP+tBW79Q+ZX9ZTt/L23MrbYYH8CNf2ayHNkip2KzusLyMw== X-Received: by 10.202.68.130 with SMTP id r124mr5713197oia.105.1456416916924; Thu, 25 Feb 2016 08:15:16 -0800 (PST) In-Reply-To: <56ce7762$0$11100$c3e8da3@news.astraweb.com> 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:103504 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. 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.