Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!news.unit0.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Namespaces are one honking great idea Date: Tue, 5 Jul 2016 12:58:19 +1000 Lines: 42 Message-ID: References: <57767a97$0$1606$c3e8da3$5496439d@news.astraweb.com> <577b1cc6$0$1584$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 2NDwtTWNE9E+cN2aCgmZIA23J6seNLeqKowcfuP0DFbQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cc:addr:python-list': 0.09; '"class"': 0.09; 'namespace': 0.09; 'def': 0.13; 'file,': 0.15; 'importing': 0.15; '.py': 0.16; '2016': 0.16; '999': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'names?': 0.16; 'namespace?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Namespaces': 0.16; 'wrote:': 0.16; 'module,': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'class,': 0.22; 'converted': 0.22; 'form:': 0.22; 'file.': 0.22; '(or': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'module.': 0.27; 'var': 0.27; 'peer': 0.29; "i'm": 0.30; 'code': 0.30; 'push': 0.30; 'another': 0.32; 'statement': 0.32; "d'aprano": 0.33; 'foo': 0.33; 'steven': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'requiring': 0.35; 'should': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'your': 0.60; 'more': 0.63; 'natural': 0.67; 'jul': 0.72; 'lives': 0.72; 'chrisa': 0.84; 'reasons:': 0.84; 'to:none': 0.91; '***': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=N32WdpbSh3VOI5PrTLfUVtp0v7Cy1+8ezUl/NYdj5Gw=; b=sAVvbiKlfXOuNDTR1uch9MTG34OB7AZ+wOyuc5GzSPfdraQl6psCgIMbgZsjwY6abS Cy1RaWNfks1huqa7QqhyfXrEZw7/T9J0J5QSf6WYiJVFU9mQYPlwkCAGIL21J98SCY+T jbqfsKpNTJfA6fn7A9jwcGLeeRDA9+VfCfDZyc84BwLEVXf1BdR/AM9QERiYQZuRUZlk G18BvRq7dNdyWpqJ1Xl4f56KeE7wg4fMbltuupOFxrfOs3WocrUKpHV4qQ0zAwndWcTY FbY/c7Pv/G4CUo6YiU1oO4EjYd3JIIVZ8+3FYqspq5mP2lTUYQhNAgAjnKiRKfANEaYf U8og== 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:from:date :message-id:subject:cc; bh=N32WdpbSh3VOI5PrTLfUVtp0v7Cy1+8ezUl/NYdj5Gw=; b=SB8w618m7byG/QwZSv0UHRQMFZXzREW9bz5/kuwO9kyWcw54F8UIS8sgY4ZvItvWqJ F6VerMzXE6NoXEm9+U6IfMbt3+fyEcD3epX0vzm1B9Gj8TqTjYIs2XGq7kwQwCiAubyE pcy33wtRidJl0XFP8DKeniYSern+uUTxZcd7JgSOaUGuKbv1Ag+vZc+Qazr17yYRq71G oJOte6aLeR+0+pQgsGPLkv20LyFLiaeJGEGZ7lU0U0bXqBzlKsViTluv9lsJXaGY0MIK 4GV7+G7lI/GmhnHvSwiYlifQA11DDWCdcwQF4VX9hQM6FsKzMl8zto/5YNO23jgIgqe4 2rAw== X-Gm-Message-State: ALyK8tInTUe6JWzmh124ioobiGqp5N6NU3hAwPtj/TwgAaSjyNr9ib/g9AvDMvGPtrgG+OYyGo+vHnMhA+xvHA== X-Received: by 10.194.139.50 with SMTP id qv18mr8871513wjb.47.1467687500011; Mon, 04 Jul 2016 19:58:20 -0700 (PDT) In-Reply-To: <577b1cc6$0$1584$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <57767a97$0$1606$c3e8da3$5496439d@news.astraweb.com> <577b1cc6$0$1584$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:111074 On Tue, Jul 5, 2016 at 12:34 PM, Steven D'Aprano wrote: > *** IF *** you are willing to push the code out into its own separate .py > file, you can use a module and write your code in a more natural form: > > > # module example.py > var = 999 > > def spam(arg): > return eggs(arg) + var > > def eggs(arg): > return arg*2 > > > What I'm calling a "namespace" is just a module object that lives inside > another module, without requiring a separate .py file. It only uses > the "class" statement for pragmatic reasons: there's no other statement > available that will do the job. If you push your code into a separate .py file, you can reference the original module by importing it. Is that also the normal way to use "outer" functions etc from inside a namespace? # demo.py pi = 3.14 def stupidfib(x): if x < 2: return x return stupidfib(x-1) + stupidfib(x-2) Namespace asdf: # (or class, however it's done) def foo(x): return stupidfib(x * pi) / pi How should foo reference those "even more global" names? "from . import pi, stupidfib" would work if you converted the module into a package ("mv demo.py demo/__init__.py"), and "from demo import pi, stupidfib" would work if you converted the namespace into a peer module. Either could make sense. ChrisA