Загрузка...

The Python Trap You NEED to Know ! #shorts #ytshorts #coding #python

Can you predict the output of this Python snippet ?

The answer is : [1], [1, 2], [3].

Why ?
In Python, default arguments are evaluated only once when the function is defined. Since a list is mutable, every call to extend() that doesn't provide its own list will keep appending to that very same original object.

When we call extend(3, []), we are passing a new empty list, so it finally creates a fresh output !

Видео The Python Trap You NEED to Know ! #shorts #ytshorts #coding #python канала CodeNFacts
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять