PYTHONL Telegram 5019
🔥 Быстро превращай словарь в именованный кортеж (`namedtuple`) — красиво, удобно, читаемо


from collections import namedtuple

Parts = {
'id_num': '1234',
'desc': 'Ford Engine',
'cost': 1200.00,
'amount': 10
}

parts = namedtuple('Parts', Parts.keys())(**Parts)
print(parts)
# Parts(amount=10, cost=1200.0, id_num='1234', desc='Ford Engine')


💡 Зачем это нужно?

Получаешь доступ к полям как к атрибутам (parts.id_num)

Удобно для структурированных данных (например, из JSON или API)

Легко отлаживать и читать

📌 Полезный приём, если хочешь избавиться от лишнего dict['ключ'] — и сделать код чуть "чище".

@pythonl
13👍7🔥4😁3



tgoop.com/pythonl/5019
Create:
Last Update:

🔥 Быстро превращай словарь в именованный кортеж (`namedtuple`) — красиво, удобно, читаемо


from collections import namedtuple

Parts = {
'id_num': '1234',
'desc': 'Ford Engine',
'cost': 1200.00,
'amount': 10
}

parts = namedtuple('Parts', Parts.keys())(**Parts)
print(parts)
# Parts(amount=10, cost=1200.0, id_num='1234', desc='Ford Engine')


💡 Зачем это нужно?

Получаешь доступ к полям как к атрибутам (parts.id_num)

Удобно для структурированных данных (например, из JSON или API)

Легко отлаживать и читать

📌 Полезный приём, если хочешь избавиться от лишнего dict['ключ'] — и сделать код чуть "чище".

@pythonl

BY Python/ django




Share with your friend now:
tgoop.com/pythonl/5019

View MORE
Open in Telegram


Telegram News

Date: |

Members can post their voice notes of themselves screaming. Interestingly, the group doesn’t allow to post anything else which might lead to an instant ban. As of now, there are more than 330 members in the group. 5Telegram Channel avatar size/dimensions How to Create a Private or Public Channel on Telegram? A Hong Kong protester with a petrol bomb. File photo: Dylan Hollingsworth/HKFP. Step-by-step tutorial on desktop:
from us


Telegram Python/ django
FROM American