Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ethan Furman Newsgroups: comp.lang.python Subject: Re: Namespaces are one honking great idea Date: Sat, 02 Jul 2016 22:14:37 -0700 Lines: 45 Message-ID: References: <57767a97$0$1606$c3e8da3$5496439d@news.astraweb.com> <577693AC.6090807@stoneleaf.us> <5776a3ef$0$1608$c3e8da3$5496439d@news.astraweb.com> <5776C4A4.5000407@stoneleaf.us> <577713c6$0$1587$c3e8da3$5496439d@news.astraweb.com> <1467435593.1705568.654776673.4D901A33@webmail.messagingengine.com> <57788a2f$0$22141$c3e8da3$5496439d@news.astraweb.com> <57789F3D.7000809@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de bUe7KsRvBgLD2PWGKq2IDgjHUqP7uqgZqgoAGVw2z4LA== 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; '(without': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'likewise': 0.09; 'message- id:@stoneleaf.us': 0.09; 'namespace': 0.09; 'python': 0.10; 'def': 0.13; 'argument': 0.15; 'variables': 0.15; 'already,': 0.16; 'namespace,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Namespaces': 0.16; 'wrote:': 0.16; 'refers': 0.18; '>>>': 0.20; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User- Agent:1': 0.26; 'linux': 0.26; 'rest': 0.26; "skip:' 10": 0.28; 'behaviour': 0.29; '~ethan~': 0.29; 'code': 0.30; 'getting': 0.33; 'class': 0.33; "d'aprano": 0.33; 'decorators': 0.33; 'steven': 0.33; 'but': 0.36; '(3)': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; '(2)': 0.37; '(1)': 0.38; 'whatever': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'more': 0.63; 'within': 0.64; '(5)': 0.72; 'special': 0.73 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: <57788a2f$0$22141$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: <57789F3D.7000809@stoneleaf.us> X-Mailman-Original-References: <57767a97$0$1606$c3e8da3$5496439d@news.astraweb.com> <577693AC.6090807@stoneleaf.us> <5776a3ef$0$1608$c3e8da3$5496439d@news.astraweb.com> <5776C4A4.5000407@stoneleaf.us> <577713c6$0$1587$c3e8da3$5496439d@news.astraweb.com> <1467435593.1705568.654776673.4D901A33@webmail.messagingengine.com> <57788a2f$0$22141$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:110975 On 07/02/2016 08:44 PM, Steven D'Aprano wrote: > Try getting this behaviour from within a class: > > > class Food(metaclass=Namespace): > > # (1) no special decorators required > def spam(n): > return ' '.join(['spam']*n) > > # (2) can call functions from inside the namespace > breakfast = spam(5) > > # (3) no "cls" or "self" argument > def lunch(): > # (4) can access variables using their undotted name > return breakfast + ' and eggs' > > def supper(): > # (5) likewise functions (a special case of #4) > return lunch() + ' and a fried slice of spam' > > def mutate(n): > # global inside the namespace refers to the namespace, > # not the surrounding module > global breakfast > breakfast = spam(5) > Can you do all of that with an ordinary class? You can get #2 already, but not the rest (without your spiffy code ;) : Python 3.5.1+ (3.5:f840608f79da, Apr 14 2016, 12:29:06) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> class Huh: ... def blah(text): ... print('blah blah %s blah blah blah' % text) ... blah('whatever') ... blah blah whatever blah blah blah -- ~Ethan~