descriptor dilemma

Discussion in 'Python' started by john, May 4, 2005.

  1. john

    john Guest

    Hello,

    I was wondering if someone could explain the following situation to me
    please:

    >>> class C(object):

    def f(self):
    pass
    >>> c = C()
    >>> c.f

    <bound method C.f of <__main__.C object at 0x00B25950>>
    >>> C.__dict__['f'].__get__(c,C)

    <bound method C.f of <__main__.C object at 0x00B25950>>
    >>> c.f == C.__dict__['f'].__get__(c,C)

    True
    >>> c.f is C.__dict__['f'].__get__(c,C)

    False

    Why do c.f and C.__dict__['f'].__get__(c,C) compare as equal under ==
    but not under *is* ?

    Thanks,

    John
     
    john, May 4, 2005
    #1
    1. Advertising

  2. Yup ?!? Weird ... especially as:

    >>> id(c.f) == id(C.__dict__['f'].__get__(c,C))

    True

    I was pretty sure that 'id(a) == id(b)' iff 'a is b' ...

    I thought initially that you had two *copies* of the
    same method bot obviously it's not true ...

    SB
     
    =?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=, May 4, 2005
    #2
    1. Advertising

  3. john

    Jeff Epler Guest

    On Wed, May 04, 2005 at 09:14:18AM -0700, Sébastien Boisgérault wrote:
    >
    > Yup ?!? Weird ... especially as:
    >
    > >>> id(c.f) == id(C.__dict__['f'].__get__(c,C))

    > True


    Here, c.f is discarded by the time the right-hand-side of == is
    executed. So the object whose id() is being calculated on the
    right-hand-side could turn out to be the same, since the two objects
    have disjoint lifetimes.

    Here are some more cases of the same thing:
    >>> id([]) == id([])

    1
    >>> id([]) == id([1])

    1

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (GNU/Linux)

    iD8DBQFCePZ0Jd01MZaTXX0RAorrAJ40RCbKQudlFZ66TuRRix/Uv10e/gCghqVd
    pRQFX+sxAR09/uyuKn2mkRI=
    =BTaK
    -----END PGP SIGNATURE-----
     
    Jeff Epler, May 4, 2005
    #3
  4. john

    Peter Otten Guest

    john wrote:

    > Why do c.f and C.__dict__['f'].__get__(c,C) compare as equal under ==
    > but not under *is* ?


    These variations are equivalent. Every attribute access gives you a new
    bound method:

    >>> class C(object):

    .... def f(self): pass
    ....
    >>> c = C()
    >>> c.f is c.f

    False
    >>> c.f == c.f

    True

    'a is b' is true only when the names a and b are bound to the same object.
    For bound methods, c.f is c.f == True would mean that Python would have to
    cache them, and I don't see a potential advantage of that.
    'a == b' on the other hand does what the programmer specifies:

    >>> class Str(str):

    .... def __eq__(self, other): return True
    ....
    >>> Str("black") == Str("blue")

    True

    It makes sense for bound methods to compare equal if they refer to the same
    function and instance (though I don't know if the class is checked, too).

    Peter
     
    Peter Otten, May 4, 2005
    #4
  5. Jeff Epler wrote:
    > > >>> id(c.f) == id(C.__dict__['f'].__get__(c,C))

    > > True

    >
    > Here, c.f is discarded by the time the right-hand-side of == is
    > executed. So the object whose id() is being calculated on the
    > right-hand-side could turn out to be the same, since the two objects
    > have disjoint lifetimes.


    Understood. I guess I was influenced by C++ where a temporary survives
    up to the end of the complete statement in which it was created. Right
    ?

    SB
     
    =?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=, May 4, 2005
    #5
    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. Chris
    Replies:
    0
    Views:
    417
    Chris
    Nov 10, 2003
  2. Kevin Spencer

    Events Dilemma

    Kevin Spencer, Oct 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    Bishop
    Oct 25, 2004
  3. =?Utf-8?B?RGVtZXRyaQ==?=

    Web Service Dilemma

    =?Utf-8?B?RGVtZXRyaQ==?=, Nov 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    312
    =?Utf-8?B?RGVtZXRyaQ==?=
    Nov 29, 2004
  4. Don Parker
    Replies:
    1
    Views:
    369
    Eliyahu Goldin
    Jan 20, 2005
  5. =?Utf-8?B?cm9kY2hhcg==?=

    summary dilemma

    =?Utf-8?B?cm9kY2hhcg==?=, Oct 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    352
    =?Utf-8?B?cm9kY2hhcg==?=
    Oct 8, 2005
Loading...

Share This Page