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

Michael Hudson mwh at python.net
Fri Feb 20 06:55:38 EST 2004


Shalabh Chaturvedi <shalabh at cafepy.com> writes:

> Almost everwhere the descriptor protocol is mentioned, it specifies
> __get__(obj, typ=None).

Really?  I thought it was "__get__(obj, cls=None)" <wink>.

> 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.

Cheers,
mwh

-- 
  Our Constitution never promised us a good or efficient government,
  just a representative one. And that's what we got.
      -- http://www.advogato.org/person/mrorganic/diary.html?start=109



More information about the Python-list mailing list