I'm trying to build some kind of generic wrapper class to hold variable objects.
class Var:
def __init__(self, obj=None):
self.obj=obj
def __getattr__(self, attr):
return getattr(self.obj, attr)
Now for example, when I try to use it with list
as obj
, I get different behavior when calling __len__
attribute or len
built-in function.
v1 = Var([1,2,3])
print(v1.__len__())
# output: 3
print(len(v1))
# output: TypeError: object of type 'Var' has no len()
That happens with other built-in functions as well. My questions are:
What in those functions implementation is causing the different behavior.
How can I bring
Var
class to work withlen
as well.