微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python custom modify the __add__ method All In One

Python custom modify the add method All In One

Python 改写 __add__方法


"""



# class Juice:
#     def __init__(self, name, capacity):
#         self.name = name
#         self.capacity = capacity

#     def __str__(self):
#         return (self.name + ' ('+str(self.capacity)+'L)')
#     # 改写 add
#     def __add__(self, other):
#         names = Juice(self.name) + '&' + Juice(other.name)
#         capacity = ' (' + str(Juice(self.capacity + Juice(other.capacity)) + 'L)'
#         return Juice(names + capacity)
#         # return f"{self.name}&{other.name}({self.capacity + other.capacity}L)"

# class Juice:
#     def __init__(self, name, capacity):
#         self.name = name
#         self.capacity = capacity

#     def __str__(self):
#         return (self.name + ' ('+str(self.capacity)+'L)')
#     # 改写 add
#     def __add__(self, other):
#         return (Juice(self.name + '&' + other.name + ' (' + str(self.capacity + other.capacity) + 'L)'))
#         # return f"{self.name}&{other.name}({self.capacity + other.capacity}L)"



https://www.sololearn.com/learning/eom-project/1073/357
https://github.com/xgqfrms/Python/blob/gh-pages/python3.x/juice-maker.py

"""


??? Python def function with __init__

MMP 纯属误导呀! --init--


def concatenate(*args):
    #  *args
    --init--
      s = ""
      l = len(list(args))
      for arg in args:
          s += (arg + '-')
      

print(concatenate("I", "love", "Python", "!"))

solution

def concatenate(*args):
    #  *args
    s = ""
    for arg in args:
        s += (arg + '-')
    
    l = len(s)
    return s[0:l - 1]
    # return s.slice(0, l - 1)

print(concatenate("I", "love", "Python", "!"))

# https://www.sololearn.com/learning/eom-project/1073/358

refs

https://www.codingem.com/python-add-method/

https://stackoverflow.com/questions/46885618/using-add-operator-with-multiple-arguments-in-python

https://stackoverflow.com/questions/40770632/typeerror-unsupported-operand-types-for



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐