[Python-3000] default argument surprises
Chris Rebert
cvrebert at gmail.com
Wed Aug 27 05:31:13 CEST 2008
You might then be interested in the following related discussions from
last year wherein I proposed something extremely similar:
[Python-ideas] proto-PEP: Fixing Non-constant Default Arguments
http://mail.python.org/pipermail/python-ideas/2007-January/000121.html
[Python-3000] pre-PEP: Default Argument Expressions
http://mail.python.org/pipermail/python-3000/2007-February/005712.html
The proposal was rejected by the BDFL (for being too magical) before
it had become a full-fledged PEP.
- Chris
On Tue, Aug 26, 2008 at 8:21 PM, Sam Bishop <samuel.j.bishop at gmail.com> wrote:
> Hi, all.
>
> I know that Python 3.0 is quite a ways along, but I'd like to make two
> small suggestions with regards to how the language works. (I would
> have spoken up earlier, except that I'm very new to Python.)
>
> I've talked to a few of my co-workers who are also new to Python, and
> we've all been surprised at the way code like this behaves:
>
> :def process(L=[]):
> : # 'x' always needs processed...
> : L += ['x']
> : print L
> :
> :process() # prints "['x']"
> :process() # prints "['x', 'x']"!
>
> We've been able to figure out what's happening, but it seems counter-intuitive.
>
> If Python were changed so that default-argument, rvalue objects were
> recreated each time a function is invoked, I think that it would be
> intuitive (and efficient) if the object were only created when
> necessary. Here's (approximately) another code snippet I've seen
> recently:
>
> :def determine_default():
> : print "Why is this code executing?"
> : return 1
> :
> :def fun(arg=determine_default()):
> : pass
> :
> :fun("value")
>
> Thanks,
> Sam
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/cvrebert%40gmail.com
>
More information about the Python-3000
mailing list