• Python的return self和return一个新的对象区别


    目的:设计一个有理数相加。如3/5 + 7/15 = 80/75

    return self

    输入:

    class Rational0:
        def __init__(self, num, den=1):
            self.num = num
            self.den = den
    
        def plus(self, another):
            den = self.den * another.den
            num = (self.num * another.den + self.den * another.num)
            return self
    
        def print(self):
            print(str(self.num) + "/" + str(self.den))
    

    输出:

    >>> r1 = Rational0(3, 5)
    >>> r1.print()
    3/5
    >>> r1
    <__main__.Rational0 object at 0x000001D1D91A5F60>
    >>> r2 = r1.plus(Rational0(7,15))     # 新对象r2
    >>> r2
    <__main__.Rational0 object at 0x000001D1D91A5F60>     # 地址相同
    >>> r1.plus(Rational0(7,15)).print()   #或者r2.print()
    3/5
    >>> r2.num
    3
    >>> r2.den
    5
    

    从地址中可以看出Rational0(3, 5)r1.plus(Rational0(7,15))返回的对象是同一个r1地址,原因在于return self

    return self做出修改

    class Rational0:
        def __init__(self, num, den=1):
            self.num = num
            self.den = den
    
        def plus(self, another):
            den = self.den * another.den 
            num = (self.num * another.den + self.den * another.num)
            self.den = den
            self.num = num
            return self
            #return Rational0(num, den)
    
        def print(self):
            print(str(self.num) + "/" + str(self.den))
    
    r1 = Rational0(3, 5)
    r2 = r1.plus(Rational0(7,15))
    r2.print()
    

    输出:

    >>> r1 = Rational0(3, 5)
    >>> r1.print()
    3/5
    >>> r1
    <__main__.Rational0 object at 0x00000239CADF5E80>
    >>> r2 = r1.plus(Rational0(7,15))
    >>> r2.print()
    80/75
    >>> r2
    <__main__.Rational0 object at 0x00000239CADF5E80>
    >>> r2.num
    80
    >>> r2.den
    75
    

    分析:


    return新的对象

    输入:

    class Rational0:
        def __init__(self, num, den=1):
            self.num = num
            self.den = den
    
        def plus(self, another):
            den = self.den * another.den
            num = (self.num * another.den + self.den * another.num)
            return Rational0(num, den)
    
        def print(self):
            print(str(self.num) + "/" + str(self.den))
    

    输出:

    >>> r1 = Rational0(3, 5)
    >>> r1.print()
    3/5
    >>> r1
    <__main__.Rational0 object at 0x000001F857C35DD8>
    >>> r2 = r1.plus(Rational0(7,15))     # 新对象r2
    <__main__.Rational0 object at 0x000001F857C48940>  #地址不同
    >>>r2.print()
    80/75
    >>> r2.num
    80
    >>> r2.den
    75
    

    从地址中可以看出Rational0(3, 5)r1.plus(Rational0(7,15))返回的对象不是同一个,原因在于return Rational0(num, den)

  • 相关阅读:
    a
    QR Code Error Correction
    Beating JSON performance with Protobuf https://auth0.com/blog/beating-json-performance-with-protobuf/
    公共错误码
    风险识别系统-大数据智能风控管理平台-企业风控解决方案– 阿里云 https://www.aliyun.com/product/saf
    python3 源码阅读-虚拟机运行原理
    Crypto.getRandomValues()
    Profile Guided Optimization Link Time Optimization
    需要更多从文献资料中借鉴他人的方法与成果
    RSA2对于所有商户都是单独一对一的,并且只支持开发平台密钥管理和沙箱
  • 原文地址:https://www.cnblogs.com/HongjianChen/p/9609333.html
Copyright © 2020-2023  润新知