PYTHON_JOB_INTERVIEW Telegram 1081
🧠 Задача с подвохом: Что выведет код?


def extendList(val, list=[]):
list.append(val)
return list

list1 = extendList(10)
list2 = extendList(123, [])
list3 = extendList('a')

print("list1 =", list1)
print("list2 =", list2)
print("list3 =", list3)


Варианты ответа:

A.

list2 = [123]
list3 = ['a']


B.

list2 = [123]
list3 = [10, 'a']


C.

list2 = [123]
list3 = [10, 'a']


D.

list2 = [123]
list3 = ['a']


Как думаешь, какой ответ правильный и почему?

Подвох: аргументы по умолчанию в Python вычисляются один раз — при определении функции.
🔸 В extendList(val, list=[]) — этот list=[] сохраняется один и тот же объект списка для всех вызовов функции, где не передаётся list.



Что происходит:
list1 = extendList(10)
→ list=[] по умолчанию
→ list = [10]
→ list1 → [10]

list2 = extendList(123, [])
→ передали новый список
→ list = [123]
→ list2 → [123]

list3 = extendList('a')
→ снова использован тот же список, что и в list1
→ list = [10, 'a']
→ list3 → [10, 'a']
→ и list1 тоже теперь [10, 'a'], потому что это один и тот же объект

Вывод будет:

list1 = [10, 'a']
list2 = [123]
list3 = [10, 'a']
👍10🔥72



tgoop.com/python_job_interview/1081
Create:
Last Update:

🧠 Задача с подвохом: Что выведет код?


def extendList(val, list=[]):
list.append(val)
return list

list1 = extendList(10)
list2 = extendList(123, [])
list3 = extendList('a')

print("list1 =", list1)
print("list2 =", list2)
print("list3 =", list3)


Варианты ответа:

A.

list2 = [123]
list3 = ['a']


B.

list2 = [123]
list3 = [10, 'a']


C.

list2 = [123]
list3 = [10, 'a']


D.

list2 = [123]
list3 = ['a']


Как думаешь, какой ответ правильный и почему?

Подвох: аргументы по умолчанию в Python вычисляются один раз — при определении функции.
🔸 В extendList(val, list=[]) — этот list=[] сохраняется один и тот же объект списка для всех вызовов функции, где не передаётся list.



Что происходит:
list1 = extendList(10)
→ list=[] по умолчанию
→ list = [10]
→ list1 → [10]

list2 = extendList(123, [])
→ передали новый список
→ list = [123]
→ list2 → [123]

list3 = extendList('a')
→ снова использован тот же список, что и в list1
→ list = [10, 'a']
→ list3 → [10, 'a']
→ и list1 тоже теперь [10, 'a'], потому что это один и тот же объект

Вывод будет:

list1 = [10, 'a']
list2 = [123]
list3 = [10, 'a']

BY Python вопросы с собеседований


Share with your friend now:
tgoop.com/python_job_interview/1081

View MORE
Open in Telegram


Telegram News

Date: |

A few years ago, you had to use a special bot to run a poll on Telegram. Now you can easily do that yourself in two clicks. Hit the Menu icon and select “Create Poll.” Write your question and add up to 10 options. Running polls is a powerful strategy for getting feedback from your audience. If you’re considering the possibility of modifying your channel in any way, be sure to ask your subscribers’ opinions first. Just at this time, Bitcoin and the broader crypto market have dropped to new 2022 lows. The Bitcoin price has tanked 10 percent dropping to $20,000. On the other hand, the altcoin space is witnessing even more brutal correction. Bitcoin has dropped nearly 60 percent year-to-date and more than 70 percent since its all-time high in November 2021. Members can post their voice notes of themselves screaming. Interestingly, the group doesn’t allow to post anything else which might lead to an instant ban. As of now, there are more than 330 members in the group. fire bomb molotov November 18 Dylan Hollingsworth yau ma tei A vandalised bank during the 2019 protest. File photo: May James/HKFP.
from us


Telegram Python вопросы с собеседований
FROM American