Why must Python list addition be homogenous?

前端 未结 4 1118
生来不讨喜
生来不讨喜 2020-12-30 23:31

Can anyone familiar with Python\'s internals (CPython, or other implementations) explain why list addition is required to be homogenous:

In [1]: x = [1]

In          


        
4条回答
  •  清酒与你
    2020-12-30 23:58

    These bug reports suggest that this design quirk was a mistake.

    Issue12318:

    Yes, this is the expected behavior and yes, it is inconsistent.

    It's been that way for a long while and Guido said he wouldn't do it again (it's in his list of regrets). However, we're not going to break code by changing it (list.__iadd__ working like list.extend).

    Issue575536:

    The intent was that list.__iadd__ correspond exactly to list.extend(). There's no need to hypergeneralize list.__add__() too: it's a feature that people who don't want to get surprised by Martin-like examples can avoid them by using plain + for lists.

    (Of course, there are those of us who find this behaviour quite surprising, including the developer who opened that bug report).

    (Thanks to @Mouad for finding these).

提交回复
热议问题