boundmeth1.py 598 B

123456789101112131415161718192021222324252627282930
  1. # tests basics of bound methods
  2. # uPy and CPython differ when printing a bound method, so just print the type
  3. print(type(repr([].append)))
  4. class A:
  5. def f(self):
  6. return 0
  7. def g(self, a):
  8. return a
  9. def h(self, a, b, c, d, e, f):
  10. return a + b + c + d + e + f
  11. # bound method with no extra args
  12. m = A().f
  13. print(m())
  14. # bound method with 1 extra arg
  15. m = A().g
  16. print(m(1))
  17. # bound method with lots of extra args
  18. m = A().h
  19. print(m(1, 2, 3, 4, 5, 6))
  20. # can't assign attributes to a bound method
  21. try:
  22. A().f.x = 1
  23. except AttributeError:
  24. print('AttributeError')