Metaclass conundrum - binding value from an outer scope

Skip Montanaro skip.montanaro at gmail.com
Fri Apr 21 08:31:41 EDT 2017


2017-04-20 15:55 GMT-05:00 Lele Gaifax <lele at metapensiero.it>:

> Does
>
>     underlying = getattr(SomeOtherClass, a)
>     def _meth(self, *args, _underlying=underlying):
>         return _underlying(self._instance, *args)
>
> help?
>

Hi, Lele. Long time no chat...

I thought of that, but with _underlying declared after *args I get a syntax
error in Python 2. If I move it in front of *args, the syntax error
disappears, but it gets interpreted as a the first argument of the method.
So if I call

obj.m1(4000)

it tries to call

4000(self._instance, *args)

Maybe functools.partial would be useful in this scenario, where I'm passing
the unbound method object to be called as a parameter. I'll have to play
around with that.

Skip


More information about the Python-list mailing list