Python's super() considered super!
sturlamolden
sturlamolden at yahoo.no
Fri May 27 11:11:19 EDT 2011
On 27 Mai, 16:27, sturlamolden <sturlamol... at yahoo.no> wrote:
> Assuming that 'self' will always be named
> 'self' in my code, I tend to patch __builtins__.super like this:
>
> import sys
> def super():
> self = sys._getframe().f_back.f_locals['self']
> return __builtins__.super(type(self),self)
A monkey-patch to __builtins__.super would probably also work.
Assuming the first argument to the callee is 'self' or 'cls':
import sys
_super = __builtins__.super
def monkeypatch(*args, **kwargs):
if (args == ()) and (kwargs=={}):
try:
obj = sys._getframe().f_back.f_locals['self']
except KeyError:
obj = sys._getframe().f_back.f_locals['cls']
return _super(type(obj),obj)
else:
return _super(*args, **kwargs)
class patcher(object):
def __init__(self):
__builtins__.super = monkeypatch
def __del__(self):
__builtins__.super = _super
_patch = patcher()
Sturla
More information about the Python-list
mailing list