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

Discussion in 'Python' started by Shalabh Chaturvedi, Feb 20, 2004.

  1. 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
    Shalabh Chaturvedi, Feb 20, 2004
    #1
    1. Advertising

  2. Re: Descriptors: why __get__(obj,typ=None) instead of__get__(obj,typ)

    Shalabh Chaturvedi <> 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
    Michael Hudson, Feb 20, 2004
    #2
    1. Advertising

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

    Michael Hudson wrote:

    >Shalabh Chaturvedi <> 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/
    Mike C. Fletcher, Feb 20, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alf
    Replies:
    9
    Views:
    384
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,666
    Smokey Grindel
    Dec 2, 2006
  3. length power
    Replies:
    2
    Views:
    59
    Rustom Mody
    Apr 10, 2014
  4. Skip Montanaro
    Replies:
    0
    Views:
    40
    Skip Montanaro
    Apr 10, 2014
  5. Johannes Schneider

    Re: why i have the output of [None, None, None]

    Johannes Schneider, Apr 10, 2014, in forum: Python
    Replies:
    0
    Views:
    37
    Johannes Schneider
    Apr 10, 2014
Loading...

Share This Page