ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python custom modify the __add__ method All In One

2022-08-21 03:00:08  阅读:195  来源: 互联网

标签:__ capacity name Python self modify add other


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, 禁止转载

标签:__,capacity,name,Python,self,modify,add,other
来源: https://www.cnblogs.com/xgqfrms/p/16609248.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有