There is a code:

class MyList: used = False def prepare(): def passed(name): keys = object.__dict__.keys() return not name in keys for name in list.__dict__: if passed(name): print(name, "-", list.__dict__[name]) attr = getattr(list, name) def func(self, *args, **kwargs): print(name, "<--------") return attr(self.obj, *args, **kwargs) setattr(__class__, name, func) def __init__(self, obj): self.obj = obj if not __class__.used: __class__.prepare() __class__.used = True 

This class should, at the first execution of the init method, assign itself objects of class list . As a result, the class should turn out to be completely copying its behavior list . Don't ask why I just didn't wrap the class object, I really needed it. But in practice, when executing the following code:

 obj = MyList([1, 2]) obj0 = MyList([22, 33]) print(obj, obj0) obj.reverse() obj0.reverse() obj.append(1) obj0.append(1) 

all output ends here:

 __ge__ <-------- Traceback (most recent call last): File "file.py", line 29, in <module> obj.reverse() File "file.py", line 17, in func return attr(self.obj, *args, **kwargs) TypeError: expected 1 arguments, got 0 

I could not solve this problem, so I ask for your help, Stack Overflow. Explain what it is, and how to solve it.

  • one
    Why not wrapped around the list ? - 0andriy
  • @ 0andriy This is the essence of the task. - Alexander Ivanov

1 answer 1

  def func(self, *args, name=name, attr=attr, **kwargs): print(name, attr, "<--------") return attr(self.obj, *args, **kwargs)