Skip to content

Instantly share code, notes, and snippets.

@rg3915
Created January 25, 2024 19:59
Show Gist options
  • Save rg3915/a270d0f28ffa045731e94cd898f7c5fc to your computer and use it in GitHub Desktop.
Save rg3915/a270d0f28ffa045731e94cd898f7c5fc to your computer and use it in GitHub Desktop.
unpack dict Python
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