473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

descriptor dilemma

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

Jul 19 '05 #1
4 1224

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

Jul 19 '05 #2
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-----

Jul 19 '05 #3
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

Jul 19 '05 #4

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

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John Roth | last post by:
Using Python 2.2.3, I create this script: class AnObject(object): "This might be a descriptor, but so far it doesn't seem like it" def __init__(self, somedata): self.it = somedata def...
0
by: Eric Huss | last post by:
I'm trying to write a descriptor (similar to the EiffelDescriptor example in the Demo directory). However, I noticed an odd behavior that descriptors written in pure Python mask the underlying...
8
by: Joe | last post by:
Hi, I want to use the C library function mkstemp. It returns a unix file descriptor (int) to an open file. How do I convert the file descriptor to a valid ofstream object? Thanks, Joe
3
by: gipsy boy | last post by:
Given a file descriptor (from a network socket, for instance), I want to have an iostream that reads/writes to it, so I can push and read data in the traditional way : sockStream << "<some...
4
by: CSN | last post by:
At startup this appears in the log: WARNING: dup(0) failed after 3195 successes: Bad file descriptor What does it mean? __________________________________________________
7
by: news | last post by:
Recently our mail from our e-commerce site has been rejected by AOL due to an IP block because someone was using our PHP scripts to send spam. Well, I got that fixed. But our legitimate...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
3
by: Eric Mahurin | last post by:
Is there a standard way to get a descriptor object for an arbitrary object attribute - independent of whether it uses the descriptor/ property protocol or not. I want some kind of...
3
by: akaarj | last post by:
I am using Linux gcc 4.1.1 version I have a file descriptor and I want to have a FILE* pointer to the file from the file descriptor. OR if I can have the file name using the file descriptor.....
5
by: yinglcs | last post by:
I have a c/c++ program in linux. I would like to know if I kill my program (e.g. exit(1)), will it release all the file descriptors my program held (regardless if I call close(fd) of each file...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.