default value in __init__

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Oct 10 15:20:35 CEST 2008


Duncan Booth:
> You can't just copy the default values on every call: you would still get
> people confused by the semantics whether you did a shallow or deep copy or
> as now no copy.

I think I agree.


> I don't think simply re-executing the default argument
> expression on each call works either: that would confuse at least as many
> people as the current system.

May I ask you why? I think I don't agree, but I am not sure.


> It would be possible, but extremely annoying to limit default arguments to
> being literal constants,

This is a possible solution, beside re-executing the default argument
expression on each call.


> unless you invent some kind of scheme for declaring that
> a type is safe to use as a default argument.

Well, it seems functional-style programming may become more common in
the future, and seeing languages like Scala, etc, maybe it can be
useful to add to Python some way to define immutable classes (in an
explicit way). Maybe subclasses of Immutable?


> Another option might just be to generate a warning the first time a program
> uses a not obviously immutable default.

I don't like this solution much.


> Even if you could change the behaviour of default arguments we
> would still get equivalent regular questions from the people who initialise
> class attributes with lists or dictionaries.

I have seen professional programmers too use class attributes instead
of instance ones...

Well, you can't create a fool-proof language that is useful too, but
in a language that is designed for new programmers too, like Python,
and that doesn't put the running speed as its most important feature,
then I think patching the most known and common pitfalls/traps is
useful, even if you can't patch them all (without turning the language
into something useless).

Bye,
bearophile



More information about the Python-list mailing list