/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
0.0.0
namedtuple_example/namedtuple.py
21 строка
793 B
Ilya Petrash
Добавлен namedtuple_example.
11 окт 2014, 23:07
11 окт 2014, 23:07
c73de3f
Код
Авторство
О чём код?
__author__ = 'ipetrash' if __name__ == '__main__': # TODO: https://docs.python.org/3/library/collections.html#collections.namedtuple_example # Basic example 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.__dict__))