BOOKPYTHON Telegram 3435
Распаковка параметров функций в Python 2 и 3

В Python 2 существовала интересная возможность распаковывать параметры функций прямо в их определении. Пример:


def between(x, (start, stop)):
return start < x < stop

interval = (5, 10)
print(between(2, interval)) # False
print(between(7, interval)) # True


Более того, это работало даже рекурсивно:


def determinant_2_x_2(((a, b), (c, d))):
return a * d - c * b

matrix = [
(1, 2),
(3, 4),
]
print(determinant_2_x_2(matrix)) # -2


Но начиная с Python 3, эта возможность была удалена из языка. Чтобы добиться того же результата, теперь нужно распаковывать параметры вручную:


def determinant_2_x_2(matrix):
row1, row2 = matrix
a, b = row1
c, d = row2
return a * d - c * b

matrix = [
(1, 2),
(3, 4),
]
print(determinant_2_x_2(matrix)) # -2


Удаление этой функциональности сделало код более явным и читаемым, но для любителей компактности Python 2 по-прежнему вызывает лёгкую ностальгию.

👉 @BookPython
👍9



tgoop.com/BookPython/3435
Create:
Last Update:

Распаковка параметров функций в Python 2 и 3

В Python 2 существовала интересная возможность распаковывать параметры функций прямо в их определении. Пример:


def between(x, (start, stop)):
return start < x < stop

interval = (5, 10)
print(between(2, interval)) # False
print(between(7, interval)) # True


Более того, это работало даже рекурсивно:


def determinant_2_x_2(((a, b), (c, d))):
return a * d - c * b

matrix = [
(1, 2),
(3, 4),
]
print(determinant_2_x_2(matrix)) # -2


Но начиная с Python 3, эта возможность была удалена из языка. Чтобы добиться того же результата, теперь нужно распаковывать параметры вручную:


def determinant_2_x_2(matrix):
row1, row2 = matrix
a, b = row1
c, d = row2
return a * d - c * b

matrix = [
(1, 2),
(3, 4),
]
print(determinant_2_x_2(matrix)) # -2


Удаление этой функциональности сделало код более явным и читаемым, но для любителей компактности Python 2 по-прежнему вызывает лёгкую ностальгию.

👉 @BookPython

BY Библиотека Python разработчика | Книги по питону


Share with your friend now:
tgoop.com/BookPython/3435

View MORE
Open in Telegram


Telegram News

Date: |

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. How to Create a Private or Public Channel on Telegram? With Bitcoin down 30% in the past week, some crypto traders have taken to Telegram to “voice” their feelings. When choosing the right name for your Telegram channel, use the language of your target audience. The name must sum up the essence of your channel in 1-3 words. If you’re planning to expand your Telegram audience, it makes sense to incorporate keywords into your name. To view your bio, click the Menu icon and select “View channel info.”
from us


Telegram Библиотека Python разработчика | Книги по питону
FROM American