Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. class Var:
  2. def __init__(self, obj=None):
  3. self.obj=obj
  4.  
  5. def __getattr__(self, attr):
  6. return getattr(self.obj, attr)
  7.  
  8. v1 = Var([1,2,3])
  9.  
  10. print(v1.__len__())
  11. # output: 3
  12.  
  13. print(len(v1))
  14. # output: TypeError: object of type 'Var' has no len()
  15.  
  16. class Var:
  17. def __init__(self, obj=None):
  18. self.obj=obj
  19.  
  20. def __getattr__(self, attr):
  21. return getattr(self.obj, attr)
  22.  
  23. def __len__(self):
  24. return len(self.obj)
  25.  
  26. v1 = Var([1,2,3])
  27.  
  28. print(v1.__len__())
  29. print(len(v1))
  30.  
  31. pawel@pawel-XPS-15-9570:~/test$ python len.py
  32. 3
  33. 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement