Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99201
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Todd <toddrjen@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Late-binding of function defaults (was Re: What is a function parameter =[] for?) |
| Date | Sat, 21 Nov 2015 09:38:01 +0100 |
| Lines | 102 |
| Message-ID | <mailman.32.1448095083.2291.python-list@python.org> (permalink) |
| References | <CAPTjJmpwjWnF=d6mpgbKS1biVLoR4APutgyH0n9t6CJ=Kh4dCg@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de LmkTIGnfL5OZleNnhZdkSw5H8+6xXUd2cpMBcQKIrI/w== |
| Return-Path | <toddrjen@gmail.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; 'args': 0.04; 'none,': 0.05; 'none:': 0.05; 'used.': 0.05; 'default.': 0.07; 'expressions': 0.07; 'remaining': 0.07; '*args': 0.09; '*args):': 0.09; '[])': 0.09; 'args,': 0.09; 'argument,': 0.09; 'complaining': 0.09; 'default:': 0.09; 'satisfy': 0.09; 'timeout': 0.09; 'python': 0.10; 'syntax': 0.13; 'def': 0.13; 'argument': 0.15; 'subject: \n ': 0.15; 'variables': 0.15; '"""not': 0.16; 'bad;': 0.16; 'binding,': 0.16; 'closures,': 0.16; 'default),': 0.16; 'discarded': 0.16; 'mapping,': 0.16; 'object()': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sentinel': 0.16; 'sequence,': 0.16; 'skip:j 30': 0.16; 'subject:?)': 0.16; 'syntactic': 0.16; 'wrote:': 0.16; 'nested': 0.18; 'try:': 0.18; '>': 0.18; 'email addr:gmail.com>': 0.18; 'python?': 0.18; '2015': 0.20; 'purposes': 0.20; '(the': 0.22; 'aspect': 0.22; 'function,': 0.22; 'parameter': 0.22; 'techniques,': 0.22; 'am,': 0.23; 'this:': 0.23; 'second': 0.24; '(this': 0.24; 'header:In- Reply-To:1': 0.24; "doesn't": 0.26; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'specify': 0.27; 'mode.': 0.29; 'handled': 0.29; 'raise': 0.29; 'too.': 0.30; "can't": 0.32; 'implement': 0.32; 'maybe': 0.33; 'point': 0.33; 'options': 0.33; 'problem': 0.33; 'instead,': 0.33; 'raising': 0.33; 'skip:j 20': 0.33; 'definition': 0.34; 'except': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'text': 0.35; 'nov': 0.35; 'something': 0.35; 'received:74.125.82': 0.35; "isn't": 0.35; 'problem.': 0.35; 'but': 0.36; 'too': 0.36; 'instead': 0.36; 'there': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'skip:& 10': 0.37; 'support,': 0.37; 'things': 0.38; 'late': 0.38; 'skip:z 10': 0.38; "won't": 0.38; 'skip:o 20': 0.38; 'subject:-': 0.39; 'rather': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'easy': 0.60; 'share': 0.61; 'skip:u 10': 0.61; 'body': 0.61; 'provide': 0.61; 'default': 0.61; 'skip:n 10': 0.62; 'is.': 0.63; 'times': 0.63; 'within': 0.64; 'our': 0.64; '20,': 0.66; 'binding': 0.66; 'obvious': 0.76; 'hand': 0.82; '"here\'s': 0.84; 'chrisa': 0.84; 'construct': 0.84; 'flaw,': 0.84; 'too:': 0.84 |
| 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 :content-type; bh=0MIuuZfmyDGB25tz4VSlkuXn9FrLleZvNXwbVPjPy4o=; b=fObmg9bUcTj3JRCV4Pk+dS7SSZG5Mhf94RwQXakGNRJ8QqZivoDJBVp1cOmeLtIS1y 3/nPrf21Z7Yfj6alyRQjpx2odmNh23FYwMTFnEk/0rBoq2ZXOEMIw4nHpE3phDMtCalS I3A2DykHZswg+m/LrYg6rpVmlTch+sA0qcbg/S44rQ+vEJ1inJY5CbfxIKbo5cBmSejJ OplHJ7z6S5W1w25UqJX90bjo25gUm3VtwLQqkcijSs2juv+ec4HkxFRGobfy/dlKPuPp p0JnUyZmRPP1hCmUhc8KHor2aRsJmC8mWqvqSt9DRdShUgIJm5AsP4G7DAHuaLg5BZ5I Aw1Q== |
| X-Received | by 10.194.112.131 with SMTP id iq3mr19085304wjb.35.1448095081334; Sat, 21 Nov 2015 00:38:01 -0800 (PST) |
| In-Reply-To | <CAPTjJmpwjWnF=d6mpgbKS1biVLoR4APutgyH0n9t6CJ=Kh4dCg@mail.gmail.com> |
| X-Content-Filtered-By | Mailman/MimeDel 2.1.20+ |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:99201 |
Show key headers only | View raw
On Nov 19, 2015 20:48, "Chris Angelico" <rosuav@gmail.com> wrote:
>
> On Fri, Nov 20, 2015 at 5:42 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> > BartC on the other hand is just complaining about an aspect of Python
> > that is legitimately controversial.
>
> IMO it's controversial mainly because there's an easy and obvious
> syntax for early binding, but late binding doesn't have syntactic
> support, and all the options are imperfect. Consider:
>
> def late1(x=None):
> """Terse but buggy"""
> do_stuff_with(x or [])
>
> def late2(x=None):
> """Not bad but has an extra line for each default. Also, can't take
None."""
> if x is None: x = []
> do_stuff_with(x)
>
> _SENTINEL = object()
> def late3(x=_SENTINEL):
> """Has the same global-pollution problem you get when you
> try to construct early binding from late; you can share the
> sentinel among multiple args, even multiple funcs, though"""
> if x is _SENTINEL: x = []
> do_stuff_with(x)
>
> def late4(x=object()):
> """Depends on its own name remaining bound in its scope,
> and will break if you change argument order"""
> if x is late4.__defaults__[0]: x = []
> do_stuff_with(x)
>
> And there might be other techniques, too. They all share one important
> flaw, too: When you ask for help about the function, you can't see
> what the default really is. All you see is:
>
> late1(x=None)
> late3(x=<object object at 0x7f7a80b3a080>)
>
> In the first two cases, it's not too bad; you can specify a timeout as
> either a number or as None, and if it's None (the default), the
> timeout is three times the currently estimated round trip time. No
> problem. But how would you implement something like next() in pure
> Python? If you provide _any second argument at all_, it returns that
> instead of raising StopIteration. The ONLY way that I can think of is
> to use *args notation:
>
> def next(iterator, *default):
> try:
> return iterator.__next__()
> except StopIteration:
> if default: return default[0]
> raise
>
> And while that isn't TOO bad for just one argument, it scales poorly:
>
> def foo(fixed_arg, *args):
> """Foo the fixed_arg against the spam mapping, the
> ham sequence, and the jam file."""
> args.reverse()
> spam = args.pop() if args else {}
> ham = args.pop() if args else []
> jam = args.pop() if args else open("jam.txt")
>
> Suppose, instead, that Python had a syntax like this:
>
> def foo(fixed_arg, spam=>{}, ham=>[], jam=>open("jam.txt")):
> """Foo the fixed_arg against the spam mapping, the
> ham sequence, and the jam file."""
>
> The expressions would be evaluated as closures, using the same scope
> that the function's own definition used. (This won't keep things alive
> unnecessarily, as the function's body will be nested within that same
> scope anyway.) Bikeshed the syntax all you like, but this would be
> something to point people to: "here's how to get late-binding
> semantics". For the purposes of documentation, the exact text of the
> parameter definition could be retained, and like docstrings, they
> could be discarded in -OO mode.
>
> Would this satisfy the people who get confused about "=[]"?
>
> ChrisA
Rather than a dedicated syntax, might this be something that could be
handled by a built-in decorator?
Maybe something like:
@late_binding
def myfunc(x, y=[]):
@late_binding('z')
def myfunc(x, y=expensive(), z=[]):
M = 5
@late_binding('z', vars_early=True)
def myfunc(x, y=expensive(), z=list(range(n))):
The big advantage, as shown in the last example, is that it could be
possible to specify early our late binding for variables as well.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Late-binding of function defaults (was Re: What is a function parameter =[] for?) Todd <toddrjen@gmail.com> - 2015-11-21 09:38 +0100
csiph-web