[Python-Dev] lament for the demise of unbound methods

Chris Withers chris at simplistix.co.uk
Thu Jul 4 19:20:54 CEST 2013


On 04/07/2013 18:00, Guido van Rossum wrote:
> Thanks for the code pointers. So it's all about monkeypatching. :-)

Well, that's the testfixtures use case, but for mush it's about figuring 
out whether you need to instantiate a class before calling a callable.

MyClass.a_method is a bit like a functools.partial in the mush case, if 
I can pass that object around and know what to do with it (which I can 
in Python 2) then I only have to pass that around.

In Python3, I either have to pass around the class, the method and a 
flag to indicate that a class and method are being passed, or wrap my 
own unboundmethod equivalent, meaning mush users would have to write 
method(MyClass, 'a_method') under Python 3 when they can just write 
MyClass.a_method under Python 2.

> I
> have only a little sympathy, as there still seems to be a way to do
> this, it's just less convenient. Too bad.

I don't know that Victor's suggestion will actually work in all the 
cases that MyClass.a_method.im_class does :-S

Chris

> On Thu, Jul 4, 2013 at 9:42 AM, Chris Withers <chris at simplistix.co.uk
>
>     Hi Guido,
>
>     I've bumped into this a couple of times.
>
>     First time was when I wanted to know whether what I had was a
>     classmethod, staticmethod or normal method here:
>
>     https://github.com/Simplistix/__testfixtures/blob/master/__testfixtures/replace.py#L59
>     <https://github.com/Simplistix/testfixtures/blob/master/testfixtures/replace.py#L59>
>
>     This resulted in having to trawl through __dict__ here:
>
>     https://github.com/Simplistix/__testfixtures/blob/master/__testfixtures/resolve.py#L17
>     <https://github.com/Simplistix/testfixtures/blob/master/testfixtures/resolve.py#L17>
>
>     ...rather than just using getattr.
>
>     I bumped into it again, yesterday, trying to add support for classes
>     to this lightweight dependency injection framework I'm developing:
>
>     https://github.com/Simplistix/__mush/blob/master/tests/test___runner.py#L189
>     <https://github.com/Simplistix/mush/blob/master/tests/test_runner.py#L189>
>
>     Here's my local copy of that test:
>
>     https://gist.github.com/__cjw296/db64279c69cdc0c5e112
>     <https://gist.github.com/cjw296/db64279c69cdc0c5e112>
>
>     The workaround I was playing with this morning is a wrapper so that
>     I know I have a class method, although what I really want to write
>     at this line is:
>
>     https://gist.github.com/__cjw296/db64279c69cdc0c5e112#__file-gistfile1-txt-L40
>     <https://gist.github.com/cjw296/db64279c69cdc0c5e112#file-gistfile1-txt-L40>
>
>     runner = Runner(T0, C1.meth, C2.meth1, C2.meth2)
>
>     ...but if I do that, how can the runner know that what it gets for
>     its second argument is a class method of C1?
>     (which is this case means that it should do C1().meth() rather than
>     C1.meth())
>
>     cheers,
>
>     Chris
>
>
>     On 04/07/2013 17:25, Guido van Rossum wrote:
>
>         Chris, what do you want to do with the knowledge you are seeking?
>
>         --Guido van Rossum (sent from Android phone)
>
>         On Jul 4, 2013 4:28 AM, "Chris Withers" <chris at simplistix.co.uk
>         <mailto:chris at simplistix.co.uk>
>         <mailto:chris at simplistix.co.uk
>         <mailto:chris at simplistix.co.uk>__>> wrote:
>
>              Hi All,
>
>              In Python 2, I can figure out whether I have a method or a
>         function,
>              and, more importantly, for an unbound method, I can figure
>         out what
>              class the method belongs to:
>
>          >>> class MyClass(object):
>              ...   def method(self): pass
>              ...
>          >>> MyClass.method
>         <unbound method MyClass.method>
>          >>> MyClass.method.im_class
>         <class '__main__.MyClass'>
>
>              There doesn't appear to be any way in Python 3 to do this,
>         which is
>              a little surprising and frustrating...
>
>              What am I missing here?
>
>              Chris
>
>              --
>              Simplistix - Content Management, Batch Processing & Python
>         Consulting
>                           - http://www.simplistix.co.uk
>              ___________________________________________________
>              Python-Dev mailing list
>         Python-Dev at python.org <mailto:Python-Dev at python.org>
>         <mailto:Python-Dev at python.org <mailto:Python-Dev at python.org>>
>         http://mail.python.org/____mailman/listinfo/python-dev
>         <http://mail.python.org/__mailman/listinfo/python-dev>
>         <http://mail.python.org/__mailman/listinfo/python-dev
>         <http://mail.python.org/mailman/listinfo/python-dev>>
>              Unsubscribe:
>         http://mail.python.org/____mailman/options/python-dev/____guido%40python.org
>         <http://mail.python.org/__mailman/options/python-dev/__guido%40python.org>
>         <http://mail.python.org/__mailman/options/python-dev/__guido%40python.org
>         <http://mail.python.org/mailman/options/python-dev/guido%40python.org>>
>
>
>         __________________________________________________________________________
>         This email has been scanned by the Symantec Email Security.cloud
>         service.
>         For more information please visit http://www.symanteccloud.com
>         __________________________________________________________________________
>
>
>     --
>     Simplistix - Content Management, Batch Processing & Python Consulting
>                  - http://www.simplistix.co.uk
>
>
>
>
> --
> --Guido van Rossum (python.org/~guido <http://python.org/~guido>)
> ______________________________________________________________________
> This email has been scanned by the Symantec Email Security.cloud service.
> For more information please visit http://www.symanteccloud.com
> ______________________________________________________________________

-- 
Simplistix - Content Management, Batch Processing & Python Consulting
             - http://www.simplistix.co.uk


More information about the Python-Dev mailing list