Message191870
A NamedTuple ABC doesn't have to define any API (so that part could wait?)[1]. I see it as most useful for isinstance checks. Here's a solution along those lines:
class NamedTuple(Sequence):
@classmethod
def __subclasshook__(cls, C):
if cls is NamedTuple:
if any("_fields" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
def namedtuple(...):
...
NamedTuple.register(result)
return result
(or include NamedTuple in the bases in the template)
For structseq support:
class NamedTuple(Sequence):
@classmethod
def __subclasshook__(cls, C):
if cls is NamedTuple:
if any("_fields" in B.__dict__ or # for namedtuple
"n_fields" in B.__dict__ # for structseq
for B in C.__mro__):
return True
return NotImplemented
[1] I agree there is still a problem if we define an ABC here with no API and then add some later. Then classes that inherit from NamedTuple would break later when we add an API (just `_fields`?). Not sure how much that is a concern though. Then again we could just close out issue1820 and actually establish the API. |
|
Date |
User |
Action |
Args |
2013-06-25 17:31:10 | eric.snow | set | recipients:
+ eric.snow, rhettinger, amaury.forgeotdarc, pitrou, thead, eric.araujo, zuo, pwaller, daniel.urban, santoso.wijaya, python-dev |
2013-06-25 17:31:10 | eric.snow | set | messageid: <1372181470.03.0.616740640058.issue7796@psf.upfronthosting.co.za> |
2013-06-25 17:31:10 | eric.snow | link | issue7796 messages |
2013-06-25 17:31:09 | eric.snow | create | |
|