首页 > 编程语言 >Python custom modify the __add__ method All In One

Python custom modify the __add__ method All In One

时间:2022-08-21 02:33:06浏览次数:86  
标签:__ 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

"""


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
From: https://www.cnblogs.com/xgqfrms/p/16609248.html

相关文章

  • git--提交日志规范
    对于版本控制工具来说,尤为重要的就是每次提交版本到代码库的日志撰写。清晰、规范、格式化的提交日志有助于追踪版本修改,查看历史记录等。Git不允许提交日志为空,这里推荐......
  • 删除链表结点类问题
    删除链表结点NO1.删除链表倒数第k个结点给定一个链表,删除链表的倒数第n个节点并返回链表的头指针。要求:空间复杂度\(O(1)\),时间复杂度\(O(n)\)如果倒数第k个......
  • Spring 04: IOC控制反转 + DI依赖注入
    Spring中的IOC一种思想,两种实现方式IOC(InversionofControl):控制反转,是一种概念和思想,指由Spring容器完成对象创建和依赖注入核心业务:(a)对象的创建(b)依赖的......
  • [Ynoi2016] 炸脖龙 I
    题目传送门已经能过hack,原因:做快速幂的时候需要微判一下边界。很好奇lxl为什么不卡显然区间加可用线段树做。然后操作二用扩展欧拉定理,每个\(p\)最多递归\(\log\)......
  • git--更改已提交的日志
    如何修改Git已提交的日志在某些时候,你发现了之前提交到git上的日志描述不全或者描述有误,这时你是会想要修改它的。但提交已经是push到服务器,甚至是已经有好几个提交在后......
  • [Ynoi2015] 盼君勿忘
    题传世纪诈骗题首先,所有子序列分别去重的和的意思是什么?令可重集\(S\)为序列\(a_l,a_{l+1}\dotsa_r\)的所有子序契合。假设我们有一个序列\(T\),对\(T\)去重......
  • Selenium 4 有哪些不一样?
    转载请注明出处❤️作者:测试蔡坨坨原文链接:caituotuo.top/d59b986c.html你好,我是测试蔡坨坨。众所周知,Selenium在2021年10月13号发布了Selenium4,目前最新的版本应该是Se......
  • [Ynoi2015] 此时此刻的光辉
    题传做完CF1422F再做这道题就肥肠有感觉了。如果你不想再看一题那么我就无耻推销一下我的题解。\[\text{————————我是分割线————————}\]请确保你......
  • [Ynoi2015] 我回来了
    题传7个月后再来看这道题,还是感觉太妙了。由于答案最终输出\(E\timesLen\),所以本质上是问\(\foralld\in[L,R]\)的贡献和,再进一步想,亵渎的要求就是寻找序列\[x_......
  • [Ynoi2015] 即便看不到未来
    题传\(O(10n\logn)\)能过,居然不卡常,青结了。感觉是比较套路的一道Ynoi了qwq。首先看题目,需要找的就是一段长度为\(1\dots10\)的极长连续的(公差为1)的等差数......