Learn about namedtuple in Python with syntax, examples and benefits. See how it differs from a regular tuple and various methods in it.
seen from United States
seen from China

seen from United Kingdom

seen from Guadeloupe
seen from Malaysia
seen from Malaysia

seen from Australia
seen from China
seen from United States
seen from Singapore

seen from Guadeloupe
seen from T1
seen from Australia

seen from Sweden
seen from Germany

seen from Canada
seen from Germany

seen from United Kingdom

seen from United States
seen from Italy
Learn about namedtuple in Python with syntax, examples and benefits. See how it differs from a regular tuple and various methods in it.

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
파이썬 - 클래스를 사용하지 않기
파이썬 – 클래스를 사용하지 않기
보통 객체 지향 언어를 설명할 때, 객체 지향의 기본적인 개념으로 클래스를 언급하는 경우가 많습니다. 그리고 자바나 C++ 등의 언어에서도 어떤 현실의 문제를 해결할 때, 그에 맞는 클래스부터 설계하는 방식으로 접근하기도 하지요. 파이썬 역시 객체지향 컨셉이 주가 되는 언어이며, 당연하게도 사용자가 직접 원하는 클래스를 정의하여 사용할 수 있습니다. 실제로 파이썬의 모든 것이 객체이기도 하지요.
그럼에도 불구하고 몇몇 특수한 경우를 제외하면 사실 파이썬에서 클래스를 직접 만들어서 사용하는 방식을 그리 권장하고 싶지는 않습니다.
(more…)
View On WordPress
Fixed What are "named tuples" in Python? #dev #it #asnwer
Fixed What are "named tuples" in Python? #dev #it #asnwer
What are "named tuples" in Python?
Reading the changes in Python 3.1, I found something… unexpected:
The sys.version_info tuple is now a named tuple:
I never heard about named tuples before, and I thought elements could either be indexed by numbers (like in tuples and lists) or by keys (like in dicts). I never expected they could be indexed both ways.
Thus, my questions are:
What are named…
View On WordPress
How to: What are "named tuples" in Python?
How to: What are "named tuples" in Python?
What are "named tuples" in Python?
Reading the changes in Python 3.1, I found something… unexpected:
The sys.version_info tuple is now a named tuple:
I never heard about named tuples before, and I thought elements could either be indexed by numbers (like in tuples and lists) or by keys (like in dicts). I never expected they could be indexed both ways.
Thus, my questions are:
What are named…
View On WordPress
Python で namedtuple を使ってバリューオブジェクトを作る
オブジェクトに値を内包した JavaBean 的なクラスをいちいち Python でチマチマ書いていくのも不毛だなー、と思っていたんだけど namedtuple を使うと楽にバリューオブジェクトが作れるっぽい。namedtuple は Python 2.6+ の標準ライブラリなので特にインストールなどは不要。 以下は namedtuple を使って二次元上の点 (Point) と、それを拡張した三次元上の位置 (Position) を作る例。 namedtuple を使って作ったクラスのフィールドを拡張したいときは、継承するのではなくフィールドを元に新たに作りなおす。
#!/usr/bin/env python # -*- coding: utf-8 -*- from collections import namedtuple Point = namedtuple('Point', ('x', 'y')) Position = namedtuple('Position', Point._fields + ('z',)) if __name__ == '__main__': p1 = Point(5, 10) print(p1) p2 = Position(5, 10, 15) print(p2)
実行結果は以下。
Point(x=5, y=10) Position(x=5, y=10, z=15)
尚バリューオブジェクトなのでインスタンス化した後のフィールドの再代入はできない。 メソッドを追加したいときは継承すれば良さそう。
#!/usr/bin/env python # -*- coding: utf-8 -*- from collections import namedtuple import math class Point(namedtuple('PointMixin', ('x', 'y'))): def distance(self, p2): xdiff = (self.x - p2.x) ** 2 ydiff = (self.y - p2.y) ** 2 return math.sqrt(xdiff + ydiff) if __name__ == '__main__': p1 = Point(1, 1) p2 = Point(4, 5) print(p1.distance(p2))
以下は実行結果。
5.0
便利だ。

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming