Created
January 25, 2024 19:59
-
-
Save rg3915/a270d0f28ffa045731e94cd898f7c5fc to your computer and use it in GitHub Desktop.
unpack dict Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def unpack_product_info(**kwargs): | |
""" | |
Desempacota um dicionário contendo informações sobre um produto em variáveis. | |
Parâmetros: | |
- kwargs (dict): Dicionário com chaves 'produto', 'título' e 'preço'. | |
Retorna: | |
- Tupla: Variáveis desempacotadas (produto, título, preço). | |
""" | |
product = kwargs.get('product', None) | |
title = kwargs.get('title', None) | |
price = kwargs.get('price', None) | |
return product, title, price | |
product_info_dict = {'product': 'Notebook', 'title': 'Lenovo', 'price': 2499.99} | |
# Desempacota o dicionário usando a função | |
product, title, price = unpack_product_info(**product_info_dict) | |
print("Product:", product) | |
print("Title:", title) | |
print("Price:", price) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment