Default Value

Neil Cerutti neilc at norwich.edu
Fri Jun 21 15:20:22 EDT 2013


On 2013-06-21, Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, Jun 22, 2013 at 4:26 AM, Rick Johnson
><rantingrickjohnson at gmail.com> wrote:
>> I could cast a "virtual net" over my poor lemmings before
>> they jump off the cliff by throwing an exception:
>>
>>   Traceback (most recent screw-up last):
>>    Line BLAH in SCRIPT
>>     def f(x = [None, b, [a, [4]]]):
>>   ArgumentError: No mutable default arguments allowed!
>
> So tell me, oh Great and Powerful Wizard of Rick, how is the
> interpreter supposed to know which defaults are mutable? I
> mean, it's obviously some intrinsic property of the object.
> Somehow one thing is clearly immutable, another thing clearly
> isn't. Will there be a PyObject_IsImmutable() API?
>
> Oh! I know. Function argument defaults will now be restricted
> to int/float/tuple. That would do it, right? Nobody would be
> bothered by little restrictions like that, would they.

I've been around here long enough to have even participated in
one of these discussions before.

Rick, it's not a wart, It's a gotcha.

The reason it's a gotcha is this: In order to predict what will
happen correctly, you have to have mastered three separate Python
concepts.

1. How name-binding works.
2. How argument passing works, i.e., via name-binding.
3. When default arguments are evaluated.
4. The Python object model.

OK, you have to know four things. Curses! I'll come in again.

-- 
Neil Cerutti



More information about the Python-list mailing list