Bizarre behavior with mutable default arguments

bukzor workitharder at gmail.com
Sun Dec 30 23:00:14 EST 2007


On Dec 30, 3:34 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Sun, 30 Dec 2007 12:41:57 -0800, bukzor wrote:
> > BTW, it's silly not to 'allow' globals when they're called for,
> > otherwise we wouldn't need the 'global' keyword.
>
> Nobody argues against allowing globals variables *when they're called
> for*, just pointing out that ninety-nine times out of a hundred, people
> use them when they're not called for and are positively harmful.
>
> And according to functional programmers, they're NEVER called for.
>
> --
> Steven.


I think you struck at the heart of the matter earlier when you noted
that this is the simplest way to declare a static variable in python.
Using the 'global' keyword is the other way, and is much more
explicit, and much more widely used. I also see this as the main use
of the 'notlocal' keyword to be introduced in py3k (it also fixes the
example given by Istvan above).

If the main value of this behavior is to declare a static variable, it
seems like an argument to create a more explicit syntax for static
variables. In the example above, the function only needed a static
integer, but created a one-length list instead because this quirk
doesn't work for immutable values.



More information about the Python-list mailing list