/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
namedtuple_example/namedtuple.py
23 строки
622 B
gil9red
Refactoring. namedtuple_example/namedtuple.py
12 апр 2025, 22:29
12 апр 2025, 22:29
1a6eb8a
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" from collections import namedtuple Point = namedtuple("Point", ["x", "y"]) p = Point(11, y=22) # instantiate with positional or keyword arguments print(p[0] + p[1]) # indexable like the plain tuple (11, 22) x, y = p # unpack like a regular tuple print(x, y) print(p.x + p.y) # fields also accessible by name print(p) # readable __repr__ with a name=value style print() XYZPoint = namedtuple("XYZPoint", ["x", "y", "z"]) xyz = XYZPoint(2, 2, z=5) print(xyz) print("z:", xyz.z) print("Points: x: {x}, y: {y}, z: {z}".format(**xyz._asdict()))