当前位置:   article > 正文

python-自定义个add函数玩玩-多态_python的add函数

python的add函数

函数:add(a,b,*args)

功能:返回两个及以上的数值、字符串、列表、字典相加的结果

代码:

  1. class ADD:
  2. 'override "add"'
  3. def __init__(self):
  4. return
  5. @classmethod
  6. def add(cls, a, b, *args):
  7. # 数值
  8. if type(a) in (int, float) and type(b) in (int, float):
  9. s = a + b
  10. for i in args:
  11. assert type(i) in (int, float), 'error:code 1'
  12. s += i
  13. return s
  14. # 字符串
  15. elif type(a) == type(b) == str:
  16. la, lb = list(a), list(b)
  17. la.extend(lb)
  18. for i in args:
  19. assert type(i) == str, 'error:code 2'
  20. la.extend(list(i))
  21. return ''.join(la)
  22. # 列表
  23. elif type(a) == type(b) == list:
  24. a.extend(b)
  25. for i in args:
  26. assert type(i) == list, 'error:code 3'
  27. a.extend(i)
  28. return a
  29. # 字典
  30. elif type(a) == type(b) == dict:
  31. for k, v in b.items():
  32. if k not in a.keys():
  33. a[k] = v
  34. for i in args:
  35. assert type(i) == dict, 'error:code 4'
  36. for k, v in i.items():
  37. if k not in a.keys():
  38. a[k] = v
  39. return a
  40. else:
  41. assert False, 'type error.'
  42. print(ADD.__doc__)
  43. print(ADD.add(1,2.5,8,))
  44. print(ADD.add('hello',' ','world','!'))
  45. print(ADD.add([1,2,3],[4,5,6],[7,8,9]))
  46. print(ADD.add({'1': 'a'}, {'2': 'b', '1': 'c'}, {'3': 'd'}))

运行结果: 

说明:

字典相加为自定义,非正式

写成多态形式:

参考文章:面向对象之多态理解,多态的作用与好处 - NFC - 博客园 (cnblogs.com)

代码:

  1. class ADD:
  2. def __init__(self, a, b, *args):
  3. self.a, self.b = a, b
  4. self.args = args
  5. def add(self): return
  6. class AddFactory:
  7. def __init__(self, a, b, *args):
  8. self.a, self.b = a, b
  9. self.args = args
  10. def add(self):
  11. a, b, args = self.a, self.b, self.args
  12. if type(a) in (int, float) and type(b) in (int, float) and all([type(i) in (int, float) for i in args]):
  13. na = NumberAdd(a, b, *args)
  14. return na.add()
  15. elif all([type(i) == str for i in args] + [type(a) == str, type(b) == str]):
  16. sa = StrAdd(a, b, *args)
  17. return sa.add()
  18. elif all([type(i) == list for i in args] + [type(a) == type(b) == list]):
  19. la = ListAdd(a, b, *args)
  20. return la.add()
  21. else:
  22. assert False, 'type error.'
  23. # 数值
  24. class NumberAdd(ADD):
  25. def __init__(self, a, b, *args):
  26. super().__init__(a, b, *args)
  27. def add(self):
  28. s = self.a + self.b
  29. for i in self.args:
  30. s += i
  31. return s
  32. # 字符串
  33. class StrAdd(ADD):
  34. def __init__(self, a, b, *args):
  35. super().__init__(a, b, *args)
  36. def add(self):
  37. la, lb = list(self.a), list(self.b)
  38. la.extend(lb)
  39. for i in self.args:
  40. la.extend(list(i))
  41. return ''.join(la)
  42. # 列表
  43. class ListAdd(ADD):
  44. def __init__(self, a, b, *args):
  45. super().__init__(a, b, *args)
  46. def add(self):
  47. self.a.extend(self.b)
  48. for i in self.args:
  49. self.a.extend(i)
  50. return self.a
  51. if __name__ == '__main__':
  52. numbers = AddFactory(4.5, 5.5, 2, )
  53. strs = AddFactory('hello', ' ', 'world', '!')
  54. lists = AddFactory([1, 2], [3, 4], [5])
  55. print(numbers.add())
  56. print(strs.add())
  57. print(lists.add())

运行结果:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/105508
推荐阅读
相关标签
  

闽ICP备14008679号