[Python] Why doesn’t Python 3.12.6 raise ValueError on slice assignment length mismatch?...

Discussão em 'Python' iniciado por Stack, Julho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I came across unexpected behavior in Python 3.12.6 while performing slice assignment.

    Here’s a minimal example:

    l3 = [2, 3, 4, 5]
    l3[::1] = [1, 3]
    print(l3)


    Since l3[::1] selects 4 elements ([2, 3, 4, 5]) and [1, 3] only provides 2 replacement values, I expected:

    ValueError: attempt to assign sequence of size 2 to extended slice of size 4


    Actually, in CPython 3.12.6 on Windows 11, no error is raised, and the output is:

    [1, 3]


    Has the behavior of slice assignment changed in Python 3.12.6, or is this an unintended bug? If it's a bug, should it be reported on the Python issue tracker?

    Continue reading...

Compartilhe esta Página