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


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



tg-me.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 вопросы с собеседований


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/python_job_interview/1081

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

To pay the bills, Mr. Durov is issuing investors $1 billion to $1.5 billion of company debt, with the promise of discounted equity if the company eventually goes public, the people briefed on the plans said. He has also announced plans to start selling ads in public Telegram channels as soon as later this year, as well as offering other premium services for businesses and users.

What is Secret Chats of Telegram

Secret Chats are one of the service’s additional security features; it allows messages to be sent with client-to-client encryption. This setup means that, unlike regular messages, these secret messages can only be accessed from the device’s that initiated and accepted the chat. Additionally, Telegram notes that secret chats leave no trace on the company’s services and offer a self-destruct timer.

telegram from jp


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