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


Groups > comp.lang.python > #82328

Re: When do default parameters get their values set?

Newsgroups comp.lang.python
Date 2014-12-10 19:26 -0800
References (3 earlier) <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>
Message-ID <e7f6710e-1d5c-4bcb-b7b0-56c6633f97eb@googlegroups.com> (permalink)
Subject Re: When do default parameters get their values set?
From Rustom Mody <rustompmody@gmail.com>

Show all headers | View raw


On Thursday, December 11, 2014 8:45:22 AM UTC+5:30, 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?

What? Referential transparency (or the lack of it)
https://en.wikipedia.org/wiki/Referential_transparency_%28computer_science%29

Or if you like it more technical: 
Python breaks referential transparency even in the absence of assignment

[From my (cursory) reading of the wikipedia page all the 
no-transparent examples need assignment (or IO) to demo the point]

> 
> 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.
> 
> 
> 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.
> 
> 
> 
> > Because to me 'is' -- equivalently id -- is a code-smell 
> 
> It is only a code-smell in the sense that caring about object identity 
> should be rare.

Yes. Its good we agree on that ;-)

In the FP world referential opaqueness is the name of the devil.
In the more 'real' imperative/OO world there's way too much of it.

Finding a middle-way remains an IMHO important open problem

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


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