By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,630 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

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

P: n/a
Almost everwhere the descriptor protocol is mentioned, it specifies
__get__(obj, typ=None). Why is a default value needed for the second
argument? In which case does Python call a descriptor without a second
argument?

Thanks,
Shalabh
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Shalabh Chaturvedi <sh*****@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/mrorg...html?start=109
Jul 18 '05 #2

P: n/a
Michael Hudson wrote:
Shalabh Chaturvedi <sh*****@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/

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.