Why must Python list addition be homogenous?

前端 未结 4 1119
生来不讨喜
生来不讨喜 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:43

    My guess is that Python is strongly typed, and there's not a clear indication of the right thing to do here. Are you asking Python to append the string itself, or to cast the string to a list (which is what you indicated you'd like it to do)?

    Remember explicit is better than implicit. In the most common case, neither of those guesses is correct and you're accidentally trying to do something you didn't intent. Raising a TypeError and letting you sort it out is the safest, most Pythonic thing to do here.

提交回复
热议问题