序文
Pythonicな方法でデータクラスを概念化する方法を考えていました。具体的には、DTO ( Data Transfer Object .)について話しています。
@jeff-oneill の質問「<a href="https://stackoverflow.com/questions/3357581/using-python-class-as-a-data-container/">Using Python class as a @joe-kington は組み込みのnamedtuple
.
質問
Python 2.7 ドキュメントのセクション 8.3.4 には、複数の名前付きタプルを組み合わせる方法の良い例があります。私の質問は、逆を達成する方法ですか?
例
ドキュメントの例を考慮してください:
>>> p._fields # view the field names
('x', 'y')
>>> Color = namedtuple('Color', 'red green blue')
>>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)
>>> Pixel(11, 22, 128, 255, 0)
Pixel(x=11, y=22, red=128, green=255, blue=0)
「Pixel」インスタンスから「Color」または「Point」インスタンスを推測するにはどうすればよいですか?
できればパイソンの精神で。