Descriptors: why __get__(obj, typ=None) instead of __get__(obj, typ)

Mike C. Fletcher mcfletch at rogers.com
Fri Feb 20 15:26:02 EST 2004


Michael Hudson wrote:

>Shalabh Chaturvedi <shalabh at cafepy.com> writes:
>  
>
...

>>Why is a default value needed for the second argument? In which case
>>does Python call a descriptor without a second argument?
>>    
>>
>
>It doesn't *look* like it does, ever.
>
>/However/ the wrapper for tp_descr_get (typeobject.c:wrap_descr_get)
>accepts 1 or 2 arguments translating an absent or None second argument
>to NULL, so if you want to behave like C-implemented descriptors,
>you'd better accept 1 or 2 arguments.  IOW, it's just part of the
>"descriptor protocol".
>
>As to *why* it's like this... erm, not sure about that.
>  
>
Possibly so that the interface is easier to use directly:

    for prop in obj.getProperties():
        try:
            prop.__get__( obj )
        except Exception:
           argh()

Most of the time the class argument is unused (for simple property-like 
code, anyway), so why not let people omit it when calling directly.  
Seems like a good design choice to me.

Enjoy,
Mike

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/






More information about the Python-list mailing list