Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Cameron Simpson Newsgroups: comp.lang.python Subject: Re: "from module import data; print(data)" vs "import module; print(module.data)" Date: Thu, 25 Feb 2016 12:22:36 +1100 Lines: 78 Message-ID: References: Reply-To: python-list@python.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed X-Trace: news.uni-berlin.de Ic2bLqr/4AD2E+QswTPEXgQYVYz5IXVu/zLTDLP7cMZA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python,': 0.02; 'run,': 0.07; 'cc:addr:python-list': 0.09; 'imports': 0.09; 'os.path': 0.09; 'subject:module': 0.09; "they've": 0.09; 'warn': 0.09; 'python': 0.10; '"module"': 0.16; '"x"': 0.16; '>in': 0.16; 'cc:name:python list': 0.16; 'conflicting': 0.16; 'dynamic,': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'latter,': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'python;': 0.16; 'readable': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simpson': 0.16; 'subject:import': 0.16; 'wrote:': 0.16; 'tells': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'affects': 0.22; 'latter': 0.22; 'cheers,': 0.22; '(or': 0.23; 'seems': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User- Agent:1': 0.26; 'example': 0.26; 'compare': 0.27; 'least': 0.27; 'dan': 0.29; 'does,': 0.29; 'symbols': 0.29; "i'm": 0.30; 'code': 0.30; 'saves': 0.30; 'run': 0.33; 'largely': 0.33; 'shorter': 0.33; 'symbol': 0.33; 'done': 0.35; 'clear': 0.35; 'generic': 0.35; 'path': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'there': 0.36; 'two': 0.37; 'being': 0.37; 'say': 0.37; 'charset :us-ascii': 0.37; 'things': 0.38; 'doing': 0.38; 'difference': 0.38; 'received:localdomain': 0.38; 'names': 0.38; 'goes': 0.39; 'data': 0.39; 'does': 0.39; 'subject:from': 0.39; 'area': 0.39; 'where': 0.40; 'term': 0.60; 'your': 0.60; 'making': 0.62; 'course': 0.62; 'above,': 0.63; 'leaving': 0.63; 'more': 0.63; 'different': 0.63; 'believe': 0.66; 'cameron': 0.66; 'header :Reply-To:1': 0.67; 'subject': 0.70; 'reply-to:no real name:2**0': 0.71; '(look': 0.84; '>if': 0.84; '>you': 0.84; 'reply- to:addr:python.org': 0.84; 'surface': 0.84; 'wins': 0.84; 'either:': 0.91; 'subject:; ': 0.91; 'from.': 0.93; 'increases': 0.93; 'info,': 0.93 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) 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:103474 On 24Feb2016 17:07, Dan Stromberg wrote: >Could people please compare and contrast the two ways of doing imports >in the Subject line? > >I've long favored the latter, but I'm working in a code base that >prefers the former. I largely use the former "from module import name, name..." over "import module". It makes your code more readable by making your calls to module-supplied names less verbose. That readbility does rely on the names being not too many or not to vague. As a glaring counter example to my preference, consider: from os.path import join "join" is a pretty generic name, so my code almost always goes either: import os.path and uses "os.path.join(a, b)" or (recently, in code doing a lot of path manipulation): from os.path import join as joinpath as with (real example): from os.path import abspath, normpath, join as joinpath and then using "joinpath(a, b)". >Is it fair to say that the former increases the surface area of your >shared (sometimes mutable) state? It can go the other way. Consider: import module module.data = 1 versus: from module import data data = 1 The former directly affects the name "data" in "module"; other users of "module" will see this change. The latter binds the _local_ name "data" to 1, leaving "module.data" as it was. Of course both of these are things that are almost never done (look up the term "monkey patch" for more info, but the difference is salient. >It's clear that the former saves keystrokes. >I find the latter a little more clear, because you don't have to go >look for where a symbol came from. I take the view that your should broadly know where a name came from, at least as far as what it does, and I will take shorter readably code over long.module.names.with.dots. And as mentioned above, is a name is vague (or conflicting), there is always the useful: from module import name as better_name >PS: Haskell seems better at the former than Python; Haskell tells you >if you import two identical symbols from two different places, when >you try to use one of them - not at import time. I believe in Python, >whichever symbol you import last, wins. It wins in exactly the same way that: x = 1 x = 2 wins for 2: they both run, and after they've run "x" will be bound to 2. Python is dynamic, and this is legal. It may be that linting tools like pylint will warn about conflicting names from imports. Cheers, Cameron Simpson