[Python-ideas] fixing mutable default argument values

Calvin Spealman ironfroggy at gmail.com
Thu Jan 18 05:10:30 CET 2007


On 1/17/07, Joel Bender <jjb5 at cornell.edu> wrote:
> > Where the <>s indicate these special semantics.
>
> The syntax bothers me, but launched an idea: How about separating
> parameter default value issues from instance specific definition time
> objects?
>
>      def foo(a, b=4, c=None):
>          local d = Bar([2,3,4])
>          ...
>
> The function code can decide if it wants to use c, provided by the
> caller, or d when no value for c is given.  The developer may decide
> that None is not a good value for 'no value for c', but that's a design
> decision.

I dont understand how that would be different than doing

   c = c if c is not None else Bar([2,3,4])

> You can do this now with:
>
>      def foo(a, b=4, c=None):
>          ...
>
>      foo.func_dict['d'] = Bar([2,3,4])

This would not really work in practice. See this:

>>> def f():
...     print a
...
>>> f.func_dict['a'] = 10
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in f
NameError: global name 'a' is not defined

> But I would rather see this inside foo(), jamming variables into the
> func_dict bothers me too :-).
>
> The new keyword would work for classes, but be a functional noop:
>
>      class Snorf:
>          local eggs = 3
>          spam = 4
>
>
> Joel
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>


-- 
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/



More information about the Python-ideas mailing list