class_super_closure.py 552 B

123456789101112131415161718
  1. # test that no-arg super() works when self is closed over
  2. class A:
  3. def __init__(self):
  4. self.val = 4
  5. def foo(self):
  6. # we access a member of self to check that self is correct
  7. return list(range(self.val))
  8. class B(A):
  9. def foo(self):
  10. # self is closed over because it's referenced in the list comprehension
  11. # and then super() must detect this and load from the closure cell
  12. return [self.bar(i) for i in super().foo()]
  13. def bar(self, x):
  14. return 2 * x
  15. print(A().foo())
  16. print(B().foo())