>>> class Potato(object):
... def method(self, spam):
... print self, spam
...
>>> spud = Potato()
Works:
>>> Potato.method(spud, **{'spam': 123})
<__main__.Potato object at 0x7f86cd4ee9d0> 123
Doesn't work:
>>> Potato.method(**{'self': spud, 'spam': 123})
# TypeError
But why not? I thought 'self' was just a convention, and there was nothing intrinsically special about this argument?
Python 2's instancemethod
wrapper object insists on inspecting the first positional argument, and that check does not support keyword arguments, full stop:
>>> Potato.method(self=spud, spam=123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method method() must be called with Potato instance as first argument (got nothing instead)
Note that I did not use argument unpacking there!
You can use positional arguments just fine:
>>> Potato.method(*(spud,), **{'spam': 123})
<__main__.Potato object at 0x1002b57d0> 123
or you can access the original function object:
>>> Potato.method.__func__(**{'self': spud, 'spam': 123})
<__main__.Potato object at 0x1002b57d0> 123
to bypass this limitation.
Python 3 no longer uses a method wrapper for unbound methods; the underlying function is returned directly instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With