Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82366
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone02.am1.xlned.com!bcyclone02.am1.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ckaynor@zindagigames.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'ugly': 0.07; 'objects,': 0.09; 'objects:': 0.09; 'raises': 0.09; 'subject:set': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"is"': 0.16; '(lambda': 0.16; 'a()': 0.16; 'b()': 0.16; 'behavior,': 0.16; 'distinct': 0.16; 'garbage': 0.16; 'keyerror': 0.16; 'lambda': 0.16; 'objects.': 0.16; 'objects?': 0.16; 'subject:default': 0.16; 'subject:parameters': 0.16; 'subject:their': 0.16; 'subject:values': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'demonstrate': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'work,': 0.20; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'id:': 0.23; 'question': 0.24; 'cc:2**0': 0.24; '>': 0.26; 'compare': 0.26; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; '[1]': 0.29; '[2]': 0.30; 'dec': 0.30; 'returned': 0.30; 'message- id:@mail.gmail.com': 0.30; '>>>>': 0.31; "d'aprano": 0.31; 'inspect': 0.31; 'steven': 0.31; 'this.': 0.32; 'another': 0.32; 'received:209.85.212': 0.32; 'could': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'subject:?': 0.36; 'two': 0.37; 'received:209': 0.37; 'being': 0.38; 'skip:& 10': 0.38; 'pm,': 0.38; 'that,': 0.38; 'does': 0.39; 'skip:x 10': 0.40; 'skip:\xc2 10': 0.60; 'more': 0.64; 'different': 0.65; 'natural': 0.68; 'consequences': 0.74; 'subject:get': 0.81; 'dict.': 0.84 |
| 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:to:cc:content-type; bh=zQJs41J21bqQuXAkwjRuHokg9/gKgZj6WW0uh3XdLtg=; b=fTrLfMxDrGQdzQt/fcxRfVnyoVHZhxVrwh6RnmKnuG/wXIqS/Ay+Y2hu7VoqPTYoHb c9ai7tTNhosVE+id3ubyzcehAfLDcREarQBdL2M8sxqFQEfZQp4rcZuX2qGri9lmcV1U 5xIalrmlu/A2oRy7CoR1Ecz7wYYCWe06V+jtnEu4JCxQRt+hvR1VMPXS+65H5h1cE9Nv 33UmlvlWaV8yXy1vBfY5tr55cC0eTXQ4h6EWrZ63QqiJ2a3dOPD2S+f0QtGXp8SSmQSb KftB0rGt/Tw+x2hBkPN5B1jVMtzqjo3yCahSsB48gqaLYfqNhy3CyomVw0gIPnvU7xb2 LEGA== |
| X-Gm-Message-State | ALoCoQl5+ePPu1lXQZNLN+zdM9uupheCRgMwYA3RIAO91M+eG7tzOe6N0DgVhe6B+GUmFv8wl7w+ |
| X-Received | by 10.194.134.234 with SMTP id pn10mr19475141wjb.34.1418317872034; Thu, 11 Dec 2014 09:11:12 -0800 (PST) |
| MIME-Version | 1.0 |
| In-Reply-To | <54890c3d$0$2817$c3e8da3$76491128@news.astraweb.com> |
| References | <54866F2E.1765.complang@piz.noip.com> <54866F2E.1768.complang@piz.noip.com> <XnsA3FED89759430duncanbooth@127.0.0.1> <mailman.16782.1418166477.18130.python-list@python.org> <bc561515-fe9d-41c1-bdda-7362e76aecdf@googlegroups.com> <mailman.16824.1418236733.18130.python-list@python.org> <c0d4cdaf-6d71-43ae-a998-d422fe4ecf0e@googlegroups.com> <54890c3d$0$2817$c3e8da3$76491128@news.astraweb.com> |
| From | Chris Kaynor <ckaynor@zindagigames.com> |
| Date | Thu, 11 Dec 2014 09:10:51 -0800 |
| Subject | Re: When do default parameters get their values set? |
| To | "Steven D'Aprano" <steve@pearwood.info> |
| Content-Type | multipart/alternative; boundary=089e012284e2f110be0509f3dcba |
| Cc | "python-list@python.org" <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16858.1418317880.18130.python-list@python.org> (permalink) |
| Lines | 179 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1418317880 news.xs4all.nl 2935 [2001:888:2000:d::a6]:55540 |
| X-Complaints-To | abuse@xs4all.nl |
| X-Received-Bytes | 9916 |
| X-Received-Body-CRC | 1577533533 |
| Xref | csiph.com comp.lang.python:82366 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Wed, Dec 10, 2014 at 7:15 PM, Steven D'Aprano <steve@pearwood.info>
wrote:
> On Wed, 10 Dec 2014 18:18:44 -0800, Rustom Mody wrote:
>
>
> > And going the other way -- no defs only lambdas its this:
> >
> >
> >>>> f = lambda : (lambda x= {}: x)
> >>>> f()() is f()()
> > False
> >>>> d = f()
> >>>> d() is d()
> > True
> >>>>
> >>>>
> >
> > But I have a different question -- can this be demonstrated without the
> > 'is'?
>
>
> Can *what* be demonstrated? That the functions returned are different
> objects, or that the dicts are different objects? Both? Something else?
>
> Using "is" you are demonstrating that calling the function twice returns
> two distinct objects. That is the purpose of "is", to compare object
> identity. Without "is", you can compare object IDs directly:
>
> id(f()()) == id(f()())
>
> but that's ugly and less efficient. Using "is" is the more idiomatic and
> natural way to do this.
>
In CPython, that does not work, as the dictionary will be garbage collected
after each call to id:
>>> f = lambda : (lambda x= {}: x)
>>> f()() is f()()
False
>>> id(f()()) == id(f()())
True
>
> Other than that, you could do something to demonstrate the consequences
> of the two values being distinct objects:
>
> a = f()() # Call twice to get a dict.
> b = f()()
> a['key'] = 23
> b['key'] # raises KeyError
>
>
> or
>
> a = f() # Call once to get a function.
> b = f()
> a.attribute = 23
> b.attribute # raises AttributeError
>
>
> Or you could inspect the byte-code of f and try to understand it.
>
>
Another way would be to to demonstrate the behavior, without using is,
would be to use a global counter variable:
a = 1
def f():
global a
def g(b=[a]):
return b
a += 1
return g
>>> f()()
[1]
>>> f()()
[2]
>>> a = f()
>>> a()
[3]
>>> a()
[3]
>>> b = f()
>>> b()
[4]
>>> a()
[3]
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
When do default parameters get their values set? "bSneddon" <bsneddon@1:249/999.remove-cdt-this> - 2014-12-09 11:32 -0500
Re: When do default parameters get their values set? "Chris Angelico" <chris.angelico@1:249/999.remove-cdt-this> - 2014-12-09 11:32 -0500
Re: When do default parameters get their values set? "Dave Angel" <dave.angel@1:249/999.remove-cdt-this> - 2014-12-09 11:32 -0500
Re: When do default parameters get their values set? Duncan Booth <duncan.booth@invalid.invalid> - 2014-12-09 21:18 +0000
Re: When do default parameters get their values set? random832@fastmail.us - 2014-12-09 18:07 -0500
Re: When do default parameters get their values set? Rustom Mody <rustompmody@gmail.com> - 2014-12-09 18:44 -0800
Re: When do default parameters get their values set? random832@fastmail.us - 2014-12-10 13:38 -0500
Re: When do default parameters get their values set? Rustom Mody <rustompmody@gmail.com> - 2014-12-10 18:18 -0800
Re: When do default parameters get their values set? Chris Angelico <rosuav@gmail.com> - 2014-12-11 13:24 +1100
Re: When do default parameters get their values set? Rustom Mody <rustompmody@gmail.com> - 2014-12-10 18:50 -0800
Re: When do default parameters get their values set? Steven D'Aprano <steve@pearwood.info> - 2014-12-11 03:15 +0000
Re: When do default parameters get their values set? Rustom Mody <rustompmody@gmail.com> - 2014-12-10 19:26 -0800
Re: When do default parameters get their values set? Ben Finney <ben+python@benfinney.id.au> - 2014-12-11 14:49 +1100
Re: When do default parameters get their values set? Chris Kaynor <ckaynor@zindagigames.com> - 2014-12-11 09:10 -0800
Re: When do default parameters get their values set? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-12 09:51 +1100
Re: When do default parameters get their values set? random832@fastmail.us - 2014-12-11 15:44 -0500
Re: When do default parameters get their values set? "Tim Chase" <tim.chase@1:249/999.remove-cdt-this> - 2014-12-09 11:32 -0500
csiph-web