Bug in python!? persistent value of an optional parameter in function!

George Sakkis george.sakkis at gmail.com
Wed Mar 7 23:01:19 EST 2007


On Mar 7, 10:14 pm, John Nagle <n... at animats.com> wrote:
> Paul Rubin wrote:
> > "C Barr Leigh" <cpblPub... at gmail.com> writes:
>
> >>Help! Have I found a serious bug?
> >>This seems like highly undesired behaviour to me. From the program
> >>below, I get output:
>
> > It is intentional, not a bug, see the docs.  Whether it's desirable is
> > a different question.
>
>     True.  It would make sense to disallow mutable values as
> initial values for optional arguments.  The present behavior
> is silly.

1. If you'd given it a little more thought than it took you to write
this, you would perhaps realize that it is in general impossible to
determine automatically whether an arbitrary object is mutable or
not.

2. Even if you could determine it, it would be overly restrictive to
disallow all mutable values from defaults. The fact that an object is
mutable doesn't mean that the function will try to mutate it:

def paintWall(ind, colormap={1:'red', 2:'blue', 5:'green'}):
    print "Let's paint the wall %s" % colormap[ind]


George




More information about the Python-list mailing list