• Python custom modify the __add__ method All In One


    Python custom modify the add method All In One

    Python 改写 __add__ 类方法

    
    #!/usr/bin/env python3
    # coding=utf-8
    
    __author__ = 'xgqfrms'
    __editor__ = 'vscode'
    __version__ = '1.0.1'
    __copyright__ = """
      Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
    """
    
    """
      /**
       *
       * @author xgqfrms
       * @license MIT
       * @copyright xgqfrms
       * @created 2022-08-20
       *
       * @description
       * @augments
       * @example
       * @link
       *
      */
    """
    
    
    
    # ✅ __add__ !== _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, otherJuice):
            names = (self.name + '&' + otherJuice.name)
            capacities = (self.capacity + otherJuice.capacity)
            # 返回一个新对象
            return Juice(names, capacities);
    
    a = Juice('Orange', 1.5)
    b = Juice('Apple', 2.0)
    
    # print(a)
    # print(a.name)
    
    # def add(a, b):
    #     return f"{a.name}&{b.name}({a.capacity + b.capacity}L)"
    
    # result = add(a, b)
    result = a + b
    print(result)
    
    
    
    #!/usr/bin/env python3
    # coding=utf-8
    
    __author__ = 'xgqfrms'
    __editor__ = 'vscode'
    __version__ = '1.0.1'
    __copyright__ = """
      Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
    """
    
    """
      /**
       *
       * @author xgqfrms
       * @license MIT
       * @copyright xgqfrms
       * @created 2022-08-20
       *
       * @description
       * @augments
       * @example
       * @link
       *
      */
    """
    
    
    """
    
    
    
    # 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

    
    #!/usr/bin/env python3
    # coding=utf-8
    
    __author__ = 'xgqfrms'
    __editor__ = 'vscode'
    __version__ = '1.0.1'
    __copyright__ = """
      Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
    """
    
    """
      /**
       *
       * @author xgqfrms
       * @license MIT
       * @copyright xgqfrms
       * @created 2022-08-20
       *
       * @description
       * @augments
       * @example
       * @link
       *
      */
    """
    
    
    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

    https://www.sololearn.com/learning/1158



    ©xgqfrms 2012-2020

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

    原创文章,版权所有©️xgqfrms, 禁止转载 ️,侵权必究⚠️!


  • 相关阅读:
    MySQL------Navicat安装与激活
    MySQL------如何将SQLServer文件数据迁移到MySQL
    WinForm------如何跳转另一个窗口,同时关闭当前窗口
    C#------如何判断输入的是否为纯数字
    WinForm------GridControl显示每行的Indicator中的行号
    WinForm------给GridControl添加搜索功能
    WinForm------分页控件dll下载地址
    WinForm------ToolTipController与GridControl的连用
    利用IE/FF的不同识别CSS来使用浏览器兼容问题
    互换两条记录中的字段值方法(有待测试,,)
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/16609248.html
Copyright © 2020-2023  润新知