[Python] print(len(a_list.append(item))) Behaves Unexpectdly? [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    I wrote this line of codes and noticed the interpreter throws an error.

    my_list = [1, 2, 3]

    print(len(my_list.append(6)))


    The above code produced the following error

    enter image description here

    This is my best guess for this problem: Theoretically speaking, my_list.append(6) must happen first and the len() function must execute next, which will send the value 4 to the print() function. Nevertheless, It looks like the len() function somehow executes before the append() function and the interpreter considered the expression my_list.append(6) as None. If you just execute print(len(my_list)) after that error you will notice that the output is 4, not 3 which means the append() function executed correctly afterwards. Does anyone has ever encountered the same issue? Why does the interpreter behaves this way?

    I am expecting an explanation for what happened or what triggers this issue.

    Continue reading...

Compartilhe esta Página