[Python-Dev] lament for the demise of unbound methods
Michael Foord
fuzzyman at voidspace.org.uk
Fri Jul 5 11:23:36 CEST 2013
On 4 Jul 2013, at 19:00, Guido van Rossum <guido at python.org> wrote:
> Thanks for the code pointers. So it's all about monkeypatching. :-) 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've also lamented the death of bound methods in Python 3 for mock "autospeccing". Autospec introspects objects and provides mock objects with the same attributes - and with the same method signatures. For methods it needs to trim the first argument (because instances are called externally without self of course). Not being able to tell the difference between a module level function and an unbound method caused some pain then. (I worked round it by flowing the information about where the object came from through the code but it did add ugliness).
Michael
> --Guido
>
> On Thu, Jul 4, 2013 at 9:42 AM, Chris Withers <chris at simplistix.co.uk> wrote:
> 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
>
> This resulted in having to trawl through __dict__ here:
>
> 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
>
> Here's my local copy of that test:
>
> 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
>
> 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>> 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>
> 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>
>
>
> ______________________________________________________________________
> 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) _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk
--
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing
http://www.sqlite.org/different.html
More information about the Python-Dev
mailing list