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']



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: |

During a meeting with the president of the Supreme Electoral Court (TSE) on June 6, Telegram's Vice President Ilya Perekopsky announced the initiatives. According to the executive, Brazil is the first country in the world where Telegram is introducing the features, which could be expanded to other countries facing threats to democracy through the dissemination of false content. How to create a business channel on Telegram? (Tutorial) Among the requests, the Brazilian electoral Court wanted to know if they could obtain data on the origins of malicious content posted on the platform. According to the TSE, this would enable the authorities to track false content and identify the user responsible for publishing it in the first place. Channel login must contain 5-32 characters Hui said the messages, which included urging the disruption of airport operations, were attempts to incite followers to make use of poisonous, corrosive or flammable substances to vandalize police vehicles, and also called on others to make weapons to harm police.
from us


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