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.

singledispatch.py 673B

1 anno fa
123456789101112131415161718192021222324252627282930313233343536373839
  1. from functools import singledispatch
  2. class Stu(object):
  3. def __init__(self, name):
  4. self.name = name
  5. def wake_up(self):
  6. print('起床')
  7. class Police(object):
  8. def __init__(self, name):
  9. self.name = name
  10. def wake_up(self):
  11. print('起床')
  12. @singledispatch
  13. def wake_up(obj):
  14. print('不处理')
  15. @wake_up.register(Stu)
  16. def wake_stu(obj):
  17. print('今天周末休息,让孩子们再睡一会')
  18. @wake_up.register(Police)
  19. def wake_police(obj):
  20. print('警察很辛苦,又要起床了')
  21. obj.wake_up()
  22. stu = Stu('小明')
  23. police = Police('小明爸爸')
  24. wake_up(stu)
  25. wake_police(police)
  26. wake_up('一个字符串')