如果想在 V2EX 获得更好的推广效果,欢迎了解 PRO 会员机制:
https://www.v2ex.com/pro/about

如果你经常使用铜币置顶主题,持有 V2EX Solana Token 会在每日签到时获得额外铜币:
https://www.v2ex.com/solana
itskingname
V2EX  ›  推广

在 Python 中, a += b 并不总是等价于 a = a + b

  •  
  •   itskingname · Jan 29, 2019 · 4734 views
    This topic created in 2835 days ago, the information mentioned may be changed or developed.

    大家经常在一些博客中看到这样的说法:

    a += 1
    

    等价于

    a = a + 1
    

    这种说法实际上并不准确。

    我们来看一个例子:

    >>> a = [1, 2, 3]
    >>> a += (4,)
    >>> a
    [1, 2, 3, 4]
    
    >>> a = [1, 2, 3]
    >>> a = a + (4,)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can only concatenate list (not "tuple") to list
    

    这里报错了,说明a += b和a = a + b并不是完全等价的。

    请点击查看完整内容:在 Python 中,a += b 并不总是等价于 a = a + b

    misaka19000
    misaka19000
    Jan 29, 2019 via Android
    1
    不错,学习了
    OysterQAQ
    OysterQAQ
    Jan 29, 2019 via iPhone
    2
    学习了,查了下,在 java 中从本质讲这两者确实是两码事
    sdijeenx
    sdijeenx
    Jan 29, 2019❤️2
    3
    是这样的。

    These methods are called to implement the augmented arithmetic assignments (+=, -=, *=, @=, /=, //=, %=, **=, <<=, >>=, &=, ^=, |=). These methods should attempt to do the operation in-place (modifying self) and return the result (which could be, but does not have to be, self). If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, if x is an instance of a class with an __iadd__() method, x += y is equivalent to x = x.__iadd__(y) . Otherwise, x.__add__(y) and y.__radd__(x) are considered, as with the evaluation of x + y. In certain situations, augmented assignment can result in unexpected errors (see Why does a_tuple[i] += [‘ item ’] raise an exception when the addition works?), but this behavior is in fact part of the data model.
    https://docs.python.org/3/reference/datamodel.html


    Why does a_tuple[i] += [‘ item ’] raise an exception when the addition works?
    This is because of a combination of the fact that augmented assignment operators are assignment operators, and the difference between mutable and immutable objects in Python.

    This discussion applies in general when augmented assignment operators are applied to elements of a tuple that point to mutable objects, but we ’ ll use a list and += as our exemplar.
    https://docs.python.org/3/faq/programming.html#faq-augmented-assignment-tuple-error
    rekulas
    rekulas
    Jan 29, 2019
    4
    这个,我觉得别人说的是数值吧,你这个改成了 dict 相加不符合别人的原意了?(非 python 研究者,这似乎是 dict 吧)
    bumz
    bumz
    Jan 29, 2019
    5
    本来就不是一回事

    只不过对于值语义的类型,这二者的结果期望是等价的

    list 是引用语义的类型,不等价
    syahd
    syahd
    Jan 29, 2019 via Android
    6
    偷换概念。。一般说的+=是数字运算,又不是其他对象。。
    helloworld000
    helloworld000
    Jan 29, 2019❤️12
    7
    现在从 python 开始学编程的人都已经不知道 数据类型这么个东西了吗。。。。
    mario85
    mario85
    Jan 30, 2019 via iPhone❤️3
    8
    推公众号的吧。
    @Livid
    itskingname
    itskingname
    OP
    Jan 30, 2019 via iPhone
    9
    @mario85 已 block
    pkookp8
    pkookp8
    Jan 30, 2019 via Android
    10
    这算是运算符重载吧
    你想写成什么样都可以
    lance6716
    lance6716
    Jan 30, 2019 via Android❤️11
    11
    @itskingname 竟然还反向 b,牛逼牛逼
    junmoxiao
    junmoxiao
    Jan 30, 2019 via Android
    12
    ,,,,,这,,,水平有点低呀
    ETiV
    ETiV
    Jan 30, 2019 via iPhone
    13
    大家还是拒绝服务攻击吧
    leido
    leido
    Jan 30, 2019 via Android
    14
    这是运算符重载,楼主偷换概念
    Trumeet
    Trumeet
    Jan 30, 2019 via Android
    15
    数据类型 数据类型
    master13
    master13
    Jan 30, 2019❤️6
    16
    那按照楼主这逻辑,a+b=b+a 也不准确,当 a 和 b 都是 str 的时候……

    玩这个有意思吗
    Qzier
    Qzier
    Jan 30, 2019 via iPhone
    17
    废话,+ 对应的是 __add__ 方法,+= 对应的是 __iadd__ 或 __iconcat__ 方法,当然不同,完全取决于你的实现!
    Qzier
    Qzier
    Jan 30, 2019 via iPhone
    18
    补充下 + 对应的方法还有 __concat__,是针对序列类型的
    imkuang
    imkuang
    Jan 30, 2019 via Android
    19
    这是 python 里面特殊的运算符重载吧,一般说的+=是针对数值运算的。按楼主这样描述,我自己重定义一下,还可以实现其他功能呢
    fan123199
    fan123199
    Jan 30, 2019
    20
    介绍知识点不错,但不要搞个大新闻(常指标题浮夸),因为这个知识点很可能是常识。
    mayne95
    mayne95
    Jan 30, 2019 via Android
    21
    大家都知道🐱和🐶生不出🐱
    cpyhaha
    cpyhaha
    Jan 30, 2019 via Android
    22
    感觉楼主的出发点还是没错的,但是这种问题一般会比较针对初学者吧!来 V2 就挨喷了
    NVDA
    NVDA
    Jan 30, 2019❤️1
    23
    V2EX 该出个 newbie 节点,把这些东西放在这个节点。
    omri
    omri
    Jan 30, 2019
    24
    学习了,感谢楼主
    msg7086
    msg7086
    Jan 30, 2019
    25
    += 和 + 本来就是两个运算符。通常为了不反直觉,都尽量把他们定义成一样的意思。
    你要是喜欢,还能把+定义成-呢。
    以前不是还有人把 true 和 false 的定义对调着玩吗?你是不是又要说 true 等价于 false 呢。
    tabris17
    tabris17
    Jan 30, 2019
    26
    a += 1
    应该等价于
    a = a + 1

    如果不等价,那就是实现上有问题,属于副作用
    Livid
    Livid
    MOD
    PRO
    Jan 30, 2019
    27
    @Mirage09 谢谢。我们有这个节点:

    /go/newbie
    Livid
    Livid
    MOD
    PRO
    Jan 30, 2019
    28
    @itskingname 营销内容只能发到这个节点:

    /go/promotions

    这个主题已经被移动。
    NVDA
    NVDA
    Jan 30, 2019 via iPhone
    29
    @Livid 还真的有😂疏忽了
    itskingname
    itskingname
    OP
    Jan 30, 2019
    30
    @Livid 感谢站长提醒。不过这篇文章我确实没有推销东西啊。博客上面的二维码是每篇文章自动添加的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1387 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 85ms · UTC 21:21 · PVG 05:21 · LAX 13:21 · JFK 16:21
    ♥ Do have faith in what you're doing.