Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7525

Re: [Python-ideas] 'Injecting' objects as function-local constants

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'else:': 0.03; 'arguments': 0.05; 'parameter': 0.05; 'way:': 0.05; 'subject:Python': 0.06; '__name__': 0.09; 'arguments,': 0.09; 'declarations': 0.09; 'from:addr:python': 0.09; 'func': 0.09; 'messing': 0.09; 'scripts,': 0.09; 'pm,': 0.10; '>>>': 0.12; 'def': 0.12; 'written': 0.14; 'wrote:': 0.14; "'__main__':": 0.16; '*any*': 0.16; 'disadvantage': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; "function's": 0.16; 'lambda': 0.16; 'logging,': 0.16; 'made-up': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'nick': 0.16; 'optimizing': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'runtime.': 0.16; 'solves': 0.16; 'subject:function': 0.16; 'verbose)': 0.16; 'argument': 0.16; "subject:' ": 0.16; 'useful,': 0.16; "wouldn't": 0.17; 'mon,': 0.17; 'functions,': 0.19; 'of.': 0.19; 'perl': 0.19; 'programming': 0.19; 'header:In-Reply-To:1': 0.21; 'seems': 0.21; '(or': 0.24; 'code': 0.24; 'received:84': 0.25; 'function': 0.25; 'parameters': 0.26; '(in': 0.26; 'problem': 0.28; 'depends': 0.29; 'variables': 0.29; 'instead': 0.29; 'least': 0.30; 'fairly': 0.30; 'binding': 0.30; 'signatures': 0.30; 'looks': 0.31; 'google': 0.31; 'print': 0.31; 'shared': 0.32; 'steven': 0.32; 'word.': 0.32; 'to:addr:python- list': 0.33; 'list': 0.33; 'list.': 0.33; 'too': 0.33; 'things': 0.33; "i've": 0.33; 'option': 0.35; 'header:User-Agent:1': 0.35; "d'aprano": 0.35; 'reply-to:addr:python.org': 0.35; 'symbol': 0.35; 'visible': 0.35; 'widespread': 0.35; 'test': 0.35; 'actual': 0.36; 'probably': 0.36; 'uses': 0.36; 'none': 0.37; 'useful': 0.37; 'hack': 0.37; 'ways': 0.37; 'another': 0.37; 'could': 0.38; 'but': 0.38; 'some': 0.38; 'doing': 0.39; 'should': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'happen': 0.60; 'more': 0.60; 'your': 0.60; 'piece': 0.63; 'harder': 0.65; 'kept': 0.67; 'header :Reply-To:1': 0.72; 'reply-to:no real name:2**0': 0.72; 'injecting': 0.84; 'locals': 0.84; 'resistance': 0.84; 'so:': 0.84; 'subject:local': 0.84; 'dutch': 0.91; 'complexity': 0.93
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AjgHAIIv9k3Unw4S/2dsb2JhbABSmBaOJHfIN4YkBJYDiw8
Date Mon, 13 Jun 2011 16:43:11 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10
MIME-Version 1.0
To python-list@python.org
Subject Re: [Python-ideas] 'Injecting' objects as function-local constants
References <20110611133028.GD2395@chopin.edu.pl> <4BDDEB96-1EBD-477C-BA08-7750AB42B579@gmail.com> <20110612222236.GA2540@chopin.edu.pl> <4DF53DF1.9060804@canterbury.ac.nz> <it4al9$3c8$1@dough.gmane.org> <4DF5B83C.9070206@pearwood.info> <BANLkTi=ROhEAb86V53nJmJoMucgR2GaEoQ@mail.gmail.com> <4DF61FCE.4050206@pearwood.info>
In-Reply-To <4DF61FCE.4050206@pearwood.info>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.177.1307979855.11593.python-list@python.org> (permalink)
Lines 71
NNTP-Posting-Host 82.94.164.166
X-Trace 1307979856 news.xs4all.nl 49180 [::ffff:82.94.164.166]:57473
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7525

Show key headers only | View raw


On 13/06/2011 15:33, Steven D'Aprano wrote:
> Nick Coghlan wrote:
>> On Mon, Jun 13, 2011 at 5:11 PM, Steven D'Aprano <steve@pearwood.info>
>> wrote:
>>> Function parameters should be kept for actual arguments, not for
>>> optimizing
>>> name look-ups.
>>
>> Still, the post-** shared state (Jan's option 2) is likely the most
>> obvious way to get early binding for *any* purpose without polluting
>> the externally visible parameter list.
>
> I wouldn't call adding even more complexity to function signatures
> "obvious", although I grant that it depends on whether you're Dutch :)
>
> Another disadvantage is that it uses a symbol instead of a word. Too
> many symbols, and your code looks like Perl (or APL). It's hard to
> google for ** to find out what it means. It's harder to talk about a
> symbol than a word. (In written text you can just write ** but in speech
> you have to use circumlocutions or made-up names like double-splat.)
>
> [...]
>> It seems like the path of least resistance to me - the prevalence of
>> the default argument hack means there's an existing, widespread
>> practice that solves real programming issues, but is flawed in some
>> ways (specifically, messing with the function's signature). Allowing
>> declarations of shared state after the keyword-only arguments seems
>> like a fairly obvious answer.
>
> The problem with injecting locals in the parameter list is that it can
> only happen at write-time. That's useful, but there's a major
> opportunity being missed: to be able to inject at runtime. You could add
> test mocks, optimized functions, logging, turn global variables into
> local constants, and probably things I've never thought of.
>
> Here's one use-case to give a flavour of what I have in mind: if you're
> writing Unix-like scripts, one piece of useful functionality is "verbose
> mode". Here's one way of doing so:
>
> def do_work(args, verbose=False):
>     if verbose:
>         pr = print
>     else:
>         pr = lambda *args: None
>     pr("doing spam")
>     spam()
>     pr("doing ham")
>     ham()
>     # and so on
>
> if __name__ == '__main__':
>     verbose = '--verbose' in sys.argv
>     do_work(my_arguments, verbose)
>
[snip]
Here's another way:

def do_work(args):
     do_work.pr("doing spam")
     spam()
     do_work.pr("doing ham")
     ham()
     # and so on

if __name__ == '__main__':
     verbose = '--verbose' in sys.argv
     if '--verbose' in sys.argv:
         do_work.pr = print
     else:
         do_work.pr = lambda func: func  # do nothing
     do_work(my_arguments)

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: [Python-ideas] 'Injecting' objects as function-local constants MRAB <python@mrabarnett.plus.com> - 2011-06-13 16:43 +0100
  Re: [Python-ideas] 'Injecting' objects as function-local constants Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-13 16:24 +0000

csiph-web