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

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.

How Does Bitcoin Mining Work?

Bitcoin mining is the process of adding new transactions to the Bitcoin blockchain. It’s a tough job. People who choose to mine Bitcoin use a process called proof of work, deploying computers in a race to solve mathematical puzzles that verify transactions.To entice miners to keep racing to solve the puzzles and support the overall system, the Bitcoin code rewards miners with new Bitcoins. “This is how new coins are created” and new transactions are added to the blockchain, says Okoro.

telegram from sg


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