[Python-ideas] Default arguments in Python - the return - running out of ideas but...

Chris Rebert pyideas at rebertia.com
Fri May 15 08:20:45 CEST 2009


> On Thu, May 14, 2009 at 9:16 PM, Tennessee Leeuwenburg
> <tleeuwenburg at gmail.com> wrote:
>>
>> A thought from another direction...
>>
>> Any chance we could have the interpreter raise a warning for the case
>>
>> def foo(a = []):
>>   #stuff
>>
>> ?
>>
>> The empty list and empty dict args would, I imagine, be the two most
>> common mistakes. Showing a warning might, at least, solve the problem of
>> people tripping over the syntax.

On Thu, May 14, 2009 at 9:31 PM, Curt Hagenlocher <curt at hagenlocher.org> wrote:
> I think this takes the discussion in a more practical direction. Imagine
> that there were a special method name __immutable__ to be implemented
> appropriately by all builtin types. Any object passed as a default argument
> would be checked to see that its type implements __immutable__ and that
> __immutable__() is True. Failure would mean a warning or even an error in
> subsequent versions.
>
> User-defined types could implement __immutable__ as they saw fit, in the
> traditional Pythonic consenting-adults-ly way.

(A) Python's new Abstract Base Classes would probably be a better way
of doing such checking rather than introducing a new special method

(B) What about having an __immutable__() that returned an immutable
version of the object if possible? Then all default arguments could be
converted to immutables at definition-time, with errors if a default
cannot be made immutable? It would eliminate the performance concerns
since the overhead would only be incurred once (when the function gets
defined), rather than with each function call.

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-ideas mailing list