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

Mike C. Fletcher mcfletch at
Fri Feb 20 21:26:02 CET 2004

Michael Hudson wrote:

>Shalabh Chaturvedi <shalabh at> 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():
            prop.__get__( obj )
        except Exception:

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.


  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list