473,563 Members | 2,709 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unexpected result when comparing method with variable

I had a program fail on me today because the following didn't work as I
expected:
class C: .... def f(self):
.... pass
.... c = C()
m = c.f
m is c.f False

I would have expected that if I set a variable equal to a bound method, that
variable, for all intents and purposes, *is* that bound method, especially
since I hadn't changed or deleted anything on the class or its instance.

The same thing happens when attempting to compare an unbound method with a
variable name bound to that unbound method:
M = C.f
M is C.f

False

If I were to guess what is going on here, I would say that the expression
c.f invokes a descriptor that manufactures a brand new "method object" each
time. The problem is, this is non-intuitive (to me) and prevented me from
doing something I thought was useful.

My use case is deferring operations till later, by placing tuples of a
method and its arguments in a list to be processed at some future time, but
doing some special-case processing only for certain methods:

deferred = []
....
deferred.append ((c.f, ('abc', 123)))

....

for method, params in deferred:
method(*params)
if method is c.f:
# handle a special case

But I can't do that special-case handling this way, I have to resort to some
other means to identify a method, "method is c.f" is always False. Which
seems strange to me.

This behavior is in Python 2.4, 2.3, and 2.2.

The workaround really awkward:

SPECIAL_METHOD = c.f
....
deferred.append ((SPECIAL_METHO D, ('abc', 123)))
....
if method is SPECIAL_METHOD:
# handle special case

If I forget at any time to use the name SPECIAL_METHOD, but resort to it's
"real" name of c.f, then the comparison fails.

So, is this a bug or expected?

If I complain about this, would I get any sympathy? ;)

regards,
David H.

Jul 18 '05 #1
1 1190
On Mon, 4 Apr 2005 23:34:41 -0400, David Handy
<da***@handysof tware.com> wrote:

I'm not sure if this is the best way. But it might work.
for method, params in deferred:
method(*params)
try:
if method.im_func is c.f.im_func:
# handle a special case
except:
if method is c.f:
# handle a special case

Cheers,
Ron

Jul 18 '05 #2

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

Similar topics

23
1882
by: Simon Wittber | last post by:
For the first time, I have been bitten by Python. The below code produces the results: False True when I initially expected the results: False False It took me a while to work out that default keyword argument values
1
3260
by: Rolf Kemper | last post by:
Dear Experts, I'm going to create an Excell spreadsheet xml. So far things work very well. But in case I add <Row> elements by a recursive template call it goes wrong. See the attached XSLT and the result. Last 30 lines of the xslt may be the most interesting ones. I'm processing it with XMLSPY. Regardless of the xslt processor (ALTOVA /...
1
9028
by: Christoph Schittko [MVP] | last post by:
I wrote a custom WMI method provider in unmanaged C++. When I access the method from C# I get an SystemException with this information: _COMPlusExceptionCode 0xe0434f4d int _HResult 0x80131501 int Message "Unexpected error " string but I know the method executed successful and ExecMethodAsync in the
6
5954
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to assign a value to them. I wanted to trap for this exception in a subclass using super but it doesn't happen. I have read Guido's tutorial on new...
2
2060
by: Dimitri Furman | last post by:
SQL Server 2000 SP4. Running the script below prints 'Unexpected': ----------------------------- DECLARE @String AS varchar(1) SELECT @String = 'z' IF @String LIKE ''
13
3584
by: bintom | last post by:
I ran the following simple code in C++ and got unexpected results: float f = 139.4; cout << f; Output: 139.399994;
2
3423
by: =?Utf-8?B?QXJtaW4gR2FsbGlrZXI=?= | last post by:
Hi I've got an unexpected error in a unit test. I want to test a activity from Windows Workflow Foundation (WF). First, I executed the test outside of the activity just in the test-init method. In this case all works fine: public void TestInitialize() {
11
2918
by: JRough | last post by:
I'm trying to use output buffering to cheat so i can print to excel which is called later than this header(). header("Content-type: application/xmsdownload"); header("Content-Disposition: attachment; header("Pragma: no-cache"); header("Expires; 0"); print "$header\n$data";
14
3591
by: Padfoot153 | last post by:
Hey, I'm getting the error: Parse error: syntax error, unexpected T_VARIABLE in /Users/Oscar/AwesomeSongz/userCake/profile.php on line 7 with this code <?php require_once("models/config.php"); function signinTimeStamp()
0
7664
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...
0
7583
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...
0
7885
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. ...
1
7638
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...
0
7948
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...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
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
1
1198
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.