Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

membership_protocol_py3.py 961B

123456789101112131415161718192021222324252627282930313233343536
  1. # pylint: disable=missing-docstring,too-few-public-methods,no-init,no-self-use,unused-argument,pointless-statement,expression-not-assigned
  2. # metaclasses that support membership test protocol
  3. class MetaIterable(type):
  4. def __iter__(cls):
  5. return iter((1, 2, 3))
  6. class MetaOldIterable(type):
  7. def __getitem__(cls, key):
  8. if key < 10:
  9. return key ** 2
  10. else:
  11. raise IndexError("bad index")
  12. class MetaContainer(type):
  13. def __contains__(cls, key):
  14. return False
  15. class IterableClass(metaclass=MetaOldIterable):
  16. pass
  17. class OldIterableClass(metaclass=MetaOldIterable):
  18. pass
  19. class ContainerClass(metaclass=MetaContainer):
  20. pass
  21. def test():
  22. 1 in IterableClass
  23. 1 in OldIterableClass
  24. 1 in ContainerClass
  25. 1 in IterableClass() # [unsupported-membership-test]
  26. 1 in OldIterableClass() # [unsupported-membership-test]
  27. 1 in ContainerClass() # [unsupported-membership-test]