Function __defaults__

Ken Seehart ken at seehart.com
Sun Apr 24 23:21:55 CEST 2011


Good point, Benjamin.  I didn't think of testing on Jython before 
answering.  For practical purposes it's a really good idea to test 
obscure features against all potential target platforms.

In this case, I would argue that**Benjamin's test demonstrates a bug in 
Jython.

One could counter by pointing out that the documentation does not 
specify that the __defaults__ attribute is writable.  However, in that 
case, Jython should explicitly make that attribute read only (as well as 
any other magic attributes where modification is silently ignored).  
That way, at least it would raise an exception in Jython.

Therefore, I would also suggest that the behavior of writable magic 
attributes should be added to the documentation and the python test suite.

On the other hand, if for some reason it is decided that support for 
such functionality is not desired in Python, then write access to such 
attributes should be deprecated and later removed.  However, I happen to 
like the ability to do this kind of thing, so I would vote for 
specifying the current CPython 3 behavior (as demonstrated in Steven's 
initial post) in the Python documentation.

On 4/24/2011 10:02 AM, Benjamin Kaplan wrote:
> On Sun, Apr 24, 2011 at 5:58 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info>  wrote:
>> Consider this in Python 3.1:
>>
>>
>>>>> def f(a=42):
>> ...     return a
>> ...
>>>>> f()
>> 42
>>>>> f.__defaults__ = (23,)
>>>>> f()
>> 23
>>
>>
>> Is this an accident of implementation, or can I trust that changing
>> function defaults in this fashion is guaranteed to work?
>>
>>
>>
>> --
>> Steven
> Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54)
> [Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_24
> Type "help", "copyright", "credits" or "license" for more information.
>>>> def f(a=42) :
> ...    return a
> ...
>>>> f()
> 42
>>>> f.__defaults__ = (23,)
>>>> f()
> 42
>
> I'm going to go with implementation detail.
>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110424/21204583/attachment.html>


More information about the Python-list mailing list