[Python-ideas] func attribute & default arg

Steven D'Aprano steve at pearwood.info
Fri May 15 00:05:20 CEST 2009


On Thu, 14 May 2009 09:23:39 pm spir wrote:

> What we need is only *caching* a default value while the function def
> is first evaluated. A possible implementation (probably requiring few
> changes) may be to add a kind of __defaults__ dict attribute to
> functions. 

You mean like this?

>>> def parrot(x=45):
...     return x+1
...
>>> parrot()
46
>>> parrot.func_defaults
(45,)
>>> parrot.func_defaults = (17,)
>>> parrot()
18

This has existed since at least Python 1.5.


[...]
> A transition phase could be planned toward cached defaults, possibly
> with proper warnings:
>
> "Warning: use of a mutable default value updated in the function
> body. From python 3.x, default values will be cached so as not to
> change between calls. 

This makes no sense.  Python already caches defaults. If you modify a 
cached object, you will see the modifications in the next call. You 
have your understanding completely backwards: what some people want is 
for Python to *stop* caching default values.

Denis, with respect, I think your idea is too raw for this list. I don't 
wish to discourage you, but should try posting to the general python 
mailing list first, for feedback and suggestions. I think this 
confusion about cached objects is the sort of fundamental 
misunderstanding that is best discussed on the general python mailing 
list rather than here.



-- 
Steven D'Aprano



More information about the Python-ideas mailing list