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: 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> <54890c3d$0$2817$c3e8da3$76491128@news.astraweb.com> From: Chris Kaynor Date: Thu, 11 Dec 2014 09:10:51 -0800 Subject: Re: When do default parameters get their values set? To: "Steven D'Aprano" Content-Type: multipart/alternative; boundary=089e012284e2f110be0509f3dcba Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --089e012284e2f110be0509f3dcba Content-Type: text/plain; charset=UTF-8 On Wed, Dec 10, 2014 at 7:15 PM, Steven D'Aprano 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] --089e012284e2f110be0509f3dcba Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On W= ed, Dec 10, 2014 at 7:15 PM, Steven D'Aprano <steve@pearwood.info> wrote:
On Wed, 10 Dec 201= 4 18:18:44 -0800, Rustom Mody wrote:


> And going the other way -- no defs only lambdas its this:
>
>
>>>> f =3D lambda : (lambda x=3D {}: x)
>>>> f()() is f()()
> False
>>>> d =3D f()
>>>> d() is d()
> True
>>>>
>>>>
>
> But I have a different question -- can this be demonstrated without th= e
> 'is'?


Can *what* be demonstrated? That the functions returned are differen= t
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 obj= ect
identity. Without "is", you can compare object IDs directly:

id(f()()) =3D=3D id(f()())

but that's ugly and less efficient. Using "is" is the more id= iomatic and
natural way to do this.

In CPython, tha= t does not work, as the dictionary will be garbage collected after each cal= l to id:
>>> f =3D lambda : (lambda x=3D {}: x)
>>> f()() is f()()
False
>>> i= d(f()()) =3D=3D id(f()())
True



Other than that, you could do something to demonstrate the consequences
of the two values being distinct objects:

a =3D f()()=C2=A0 # Call twice to get a dict.
b =3D f()()
a['key'] =3D 23
b['key']=C2=A0 # raises KeyError


or

a =3D f()=C2=A0 # Call once to get a function.
b =3D f()
a.attribute =3D 23
b.attribute=C2=A0 # 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 usi= ng is, would be to use a global counter variable:

= a =3D 1
def f():
=C2=A0 =C2=A0 global a
=C2= =A0 =C2=A0 def g(b=3D[a]):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return b
=C2=A0 =C2=A0 a +=3D 1
=C2=A0 =C2=A0 return g
=
=C2=A0>>> f()()
[1]
>>>= ; f()()
[2]
>>> a =3D f()
>>&g= t; a()
[3]
>>> a()
[3]
>= ;>> b =3D f()
>>> b()
[4]
>= >> a()
[3]

--089e012284e2f110be0509f3dcba--