473,408 Members | 1,623 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,408 software developers and data experts.

new.instancemethod as a form of partial()


I came across this while searching for a way to DIY partial(), until it
is available in 2.5

http://aspn.activestate.com/ASPN/Coo.../Recipe/229472

However, when trying for the following, it doesn't work and is
wondering if it is a bug or intended :
import operator
import new
new.instancemethod(operator.is_,None,object)(None)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in -toplevel-
new.instancemethod(operator.is_,None,object)(None)
TypeError: is_ expected 2 arguments, got 1 new.instancemethod(operator.is_,False,object)(Fals e)

True

So it seems that instancemethod() don't like "None" as the instance.

Jan 22 '06 #1
4 1508
<bo****@gmail.com> wrote:
...
So it seems that instancemethod() don't like "None" as the instance.


"bound methods" and "unbound methods" are instance of the same type,
distinguished by one thing: the im_self of an unbound method is None,
the im_self of a bound method is anything else.

So, when you pass None as the instance, instancemethod likes it just
fine... and returns an "unbound method" as the result, so you haven't
actually achieved your goal (you must still pass the first parameter
explicitly -- all you've "gained" by wrapping a function into an unbound
method is an implicit typecheck on the first argument, and if, as the
class, you're using 'object' as in your example, that's not much use
[even in other cases, it's no great shakes;-)]).
Alex
Jan 22 '06 #2

Alex Martelli wrote:
<bo****@gmail.com> wrote:
...
So it seems that instancemethod() don't like "None" as the instance.


"bound methods" and "unbound methods" are instance of the same type,
distinguished by one thing: the im_self of an unbound method is None,
the im_self of a bound method is anything else.

So, when you pass None as the instance, instancemethod likes it just
fine... and returns an "unbound method" as the result, so you haven't
actually achieved your goal (you must still pass the first parameter
explicitly -- all you've "gained" by wrapping a function into an unbound
method is an implicit typecheck on the first argument, and if, as the
class, you're using 'object' as in your example, that's not much use
[even in other cases, it's no great shakes;-)]).

thanks. So in this special case, None is being treated as a "flag"
rather than just an instance(I just read the doc) like any other
instance and the behaviour is intended. Is there any reason why it is
designed this way ?

Jan 22 '06 #3
<bo****@gmail.com> wrote:
...
thanks. So in this special case, None is being treated as a "flag"
rather than just an instance(I just read the doc) like any other
instance and the behaviour is intended. Is there any reason why it is
designed this way ?


I didn't yet know Python back when it was designed (dark ages, really),
but I assume the point was that, back then, we only had what's now the
legacy object model: bound methods could only be bound to instances,
classes were distinct from types, etc. So, an im_self that wasn't an
instance had no possible "normal" meaning, and None was a handy
placeholder. Of course, this design then couldn't be changed (nor can
it be now, until 3.0) to preserve backwards compatibility.

Guido has mused about abolishing "unbound methods" (in 3.0, I guess), so
there's hope for the future. But a more complete 'partial' is likely to
be acceptable sooner than any fix to bound/unbound methods: I suspect
the only ingredient that's missing is a generous helping of irrefutable
use cases.
Alex
Jan 22 '06 #4

Alex Martelli wrote:
Guido has mused about abolishing "unbound methods" (in 3.0, I guess), so
there's hope for the future. But a more complete 'partial' is likely to
be acceptable sooner than any fix to bound/unbound methods: I suspect
the only ingredient that's missing is a generous helping of irrefutable
use cases.

So long the new partial() has this case covered, it is fine. As it
seems that this recipe
http://aspn.activestate.com/ASPN/Coo...n/Recipe/52549 also
forgot this special case(down to the bottom, the current curry code).

Jan 22 '06 #5

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

Similar topics

7
by: Rim | last post by:
Hi, It appears to me the simplest way to add a function to a class outside of the class declaration is as follows: >>> class a(object): .... pass .... >>> def f(self): .... print 'hi'
7
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
1
by: Martin Miller | last post by:
In section "3.27 new -- Creation of runtime internal objects" of the documentation that comes with Python 2.4 it says: > instancemethod(function, instance, class) > > This function will return...
7
by: John Reese | last post by:
Why hello there ha ha. I have got in the habit of testing the types of variables with isinstance and the builtin type names instead of using the types module, as was the style back around Python...
9
by: TheSteph | last post by:
In VS 2005 Form files are in 3 parts : From1.cs, Form1.Designer.cs and Form1.resx. In VS 2003 there is only one ".cs files". How can I split my old VS2003 ".cs files" files in 2 files :...
2
by: Steven Bethard | last post by:
I'd like to be able to pickle instancemethod objects mainly because I want to be able to delay a call like ``foo(spam, badger)`` by dumping ``foo``, ``spam`` and ``badger`` to disk and loading them...
7
by: forest demon | last post by:
all i want is to do is to pass a form reference to a separate class and be able to manipulate properties/components/controls of said form. this should be as simple as passing a TextBox, Container...
2
by: Gary Brown | last post by:
Hi, I have moved some of the implementation of a form into a second file and enclosed it with "partial class ..." VS wants to give the second file its own designer form. How do I prevent that...
8
by: TomC | last post by:
I want to bypass the Windows Form Designer in VS, to create a form programmatically. The elements of the form are to be arranged in a table, and I want the size of the table (and therefore the...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.