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


Groups > comp.lang.python > #82252

Re: When do default parameters get their values set?

X-Received by 10.42.197.134 with SMTP id ek6mr5625739icb.6.1418179456504; Tue, 09 Dec 2014 18:44:16 -0800 (PST)
X-Received by 10.50.67.97 with SMTP id m1mr123320igt.1.1418179456425; Tue, 09 Dec 2014 18:44:16 -0800 (PST)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!h15no6649437igd.0!news-out.google.com!d20ni7697igz.0!nntp.google.com!h15no6649436igd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Tue, 9 Dec 2014 18:44:15 -0800 (PST)
In-Reply-To <mailman.16782.1418166477.18130.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=59.95.24.254; posting-account=mBpa7woAAAAGLEWUUKpmbxm-Quu5D8ui
NNTP-Posting-Host 59.95.24.254
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>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <bc561515-fe9d-41c1-bdda-7362e76aecdf@googlegroups.com> (permalink)
Subject Re: When do default parameters get their values set?
From Rustom Mody <rustompmody@gmail.com>
Injection-Date Wed, 10 Dec 2014 02:44:16 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:82252

Show key headers only | View raw


On Wednesday, December 10, 2014 4:38:18 AM UTC+5:30, rand...@fastmail.us wrote:
> On Tue, Dec 9, 2014, at 16:18, Duncan Booth wrote:
> > The default parameters are actually evaluated when the 'def' statement is 
> > executed and the function object is created from the default arguments
> > and 
> > the previously compiled code block.
> 
> Which means that if you execute the def statement [or lambda] more than
> once, you will get more than one instance of the default parameter.
> 
> >>> def f(): return (lambda x={}: x)
> ...
> >>> f()() is f()()
> False
> >>> g = f()
> >>> g() is g()
> True

Nice example -- thanks.
Elaborates the why of this gotcha -- a def(inition) is imperative.
From a semantic pov very clean.
From an expectation pov always surprising.

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