473,770 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1244

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)

iD8DBQFCePZ0Jd0 1MZaTXX0RAorrAJ 40RCbKQudlFZ66T uRRix/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
1421
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 __get__(self, obj, type=None): print "in get method"
0
1097
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 object they are wrapping, whereas the descriptors written in C do not. For example, taking the code from http://users.rcn.com/python/download/Descriptor.htm which implements what one would think is a straightforward implementation: class...
8
9758
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
7072
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 stuff>" < "\r\n" How is this possible with the standard header files? I need this because my objects have xml'ed << operators, but now I can't figure out how to use them on the actual network stream.
4
2452
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
6865
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 auto-generated e-mails are getting "deferred" by AOL now with an error: Deferred: Bad file descriptor I can't find anything on their support site about this, nor Googling. Any ideas?
16
3165
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 parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed inside the function. And it is for this reason that some people advocate that it is a good idea to...
3
2031
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 handle/reference/ pointer to an attribute. I know I could make my own class to do this (using the __dict__ of the object and the attribute name), but I would like to use something standard (at least a protocol) if it exists. All that is needed in the protocol...
3
1832
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.. Please help..
5
5264
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 descriptor)? Thank you.
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.