Skip to content

Instantly share code, notes, and snippets.

@kitsuyui
Created June 30, 2016 16:14
Show Gist options
  • Save kitsuyui/06a1eee2ae93f5832ef7d0774ef53346 to your computer and use it in GitHub Desktop.
Save kitsuyui/06a1eee2ae93f5832ef7d0774ef53346 to your computer and use it in GitHub Desktop.
Python 3.4.3 と 3.4.4 の間で namedtuple の挙動が微妙に変わっていた件 ref: http://qiita.com/kitsuyui/items/871adfe855127483245f
from collections import namedtuple
X = namedtuple('X', ('a', 'b'))
class Y(X):
__slots__ = ()
class Z(X):
# __slots__ = () を忘れた場合
pass
x = X(1, 2)
y = Y(1, 2)
z = Z(1, 2)
print(x._asdict())
print(hasattr(x, '__dict__'))
print(y._asdict())
print(hasattr(y, '__dict__'))
print(z._asdict())
print(hasattr(z, '__dict__'))
OrderedDict([('a', 1), ('b', 2)])
True
OrderedDict([('a', 1), ('b', 2)])
True
{}
True
OrderedDict([('a', 1), ('b', 2)])
False
OrderedDict([('a', 1), ('b', 2)])
False
OrderedDict([('a', 1), ('b', 2)])
True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment