Bug in __init__?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Mon Jan 21 17:09:29 EST 2008
On Mon, 21 Jan 2008 17:38:10 -0200, Gabriel Genellina wrote:
> En Mon, 21 Jan 2008 05:59:38 -0200, <cokofreedom at gmail.com> escribi�:
>
>> Is there no way of adding a possible warning message (that obviously
>> could be turned off) to warn users of possible problems linked to using
>> mutable types as parameters?
>>
>> Seems to me this could save users a few minutes/hours of headaches and
>> headscratching. (The biggest issue affecting new programmers these
>> days!)
>
> Most objects are mutable, such warning would happen so often that would
> become useless.
Not at all, for two reasons:
(1) Using mutable defaults in function definitions is relatively unusual.
Defaults like None, 0, 1 are far more common.
(2) We can copy the behaviour of the warnings module. Possibly even use
the warnings module. By default warnings are only shown once per session.
> The only immutable objects are numbers, strings, tuples, None and a few
> esoteric types; all other instances, including instances of all user
> defined classes, are mutable unless explicitely their attributes are
> read-only.
It is true that Python doesn't have a cheap way of recognising mutable
instances except by trying to mutate them. However, there is a case for
having it issue a warning the first time in a session it spots a default
list or dict, which would catch 99% of cases that newbies use a mutable
default.
Newbies being newbies, 30% of them will completely ignore the warning and
bitch about the "bug", but that's better than the 80% that we have now :-)
--
Steven
More information about the Python-list
mailing list