[issue7796] No way to find out if an object is an instance of a namedtuple
Andrew Barnert added the comment: I believe the structseq issues are a lot easier to solve than the appear. See #20230, which adds a _fields member to every structseq type. Having done that, a NamedTuple ABC works on them just fine. And of course duck typing them without an ABC does too. -- nosy: +abarnert ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Eric Snow added the comment: 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. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Changes by Eric Snow ericsnowcurren...@gmail.com: -- nosy: +eric.snow ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Eric Snow added the comment: It may not enough, but the use of namedtuples (vs. plain tuples) with functools.singledispatch() would be messier without a NamedTuple ABC (or other base type). Of course, when would you want to dispatch specifically on namedtuple? I can think of a few relatively weak uses, but not enough to justify the cost of establishing a common base class for namedtuple. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Raymond Hettinger raymond.hettin...@gmail.com added the comment: Detecting _fields is the simplest thing we can do right now. There are too many structseq API differences to warrant bringing them under a single ABC umbrella. -- dependencies: -Enhance Object/structseq.c to match namedtuple and tuple api resolution: later - rejected status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Jan Kaliszewski z...@chopin.edu.pl added the comment: PS. For the record: the final recipe is here: http://code.activestate.com/recipes/577629-namedtupleabc-abstract-base-class-mix-in-for-named/ -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Roundup Robot devnull@devnull added the comment: New changeset 7aa3f1f7ac94 by Raymond Hettinger in branch '3.2': Issue #7796: Add link to Jan Kaliszewski's alternate constructor and ABC for named tuples. http://hg.python.org/cpython/rev/7aa3f1f7ac94 New changeset 330d3482cad8 by Raymond Hettinger in branch 'default': Issue #7796: Add link to Jan Kaliszewski's alternate constructor and ABC for named tuples. http://hg.python.org/cpython/rev/330d3482cad8 -- nosy: +python-dev ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Jan Kaliszewski z...@chopin.edu.pl added the comment: Thank you. Raymond is against the idea so I don't know if it makes sense to create the real patch for now (it would patch the collections module and, I suppose, addming tests, docs etc.). Anyway, if somebody would be interested in the idea, the newest version of the draft is here: http://dpaste.org/qyKv/. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Changes by Santoso Wijaya santoso.wij...@gmail.com: -- nosy: +santa4nt ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Changes by Sridhar Ratnakumar sridh...@activestate.com: -- nosy: -srid ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Changes by Daniel Urban urban.dani...@gmail.com: -- nosy: +durban ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Jan Kaliszewski z...@chopin.edu.pl added the comment: On python-ideas I have proposed an ABC being also a kind of a mix-in, potentially making namedtuple subclassing (with custom methods etc.) more convenient, e.g.: class MyRecord(namedtuple.abc): _fields = 'x y z' def _my_custom_method(self): return list(self._asdict().items()) or class MyAbstractRecord(namedtuple.abc): def _my_custom_method(self): return list(self._asdict().items()) class AnotherAbstractRecord(MyAbstractRecord): def __str__(self): return '{}'.format(super().__str__()) class MyRecord2(MyAbstractRecord): _fields = 'a, b' class MyRecord3(AnotherAbstractRecord): _fields = 'p', 'q', 'r' Here is an experimental monkey-patcher adding the 'abc' attribute to namedtuple: http://dpaste.org/T9w6/ I am not sure if it is worth preparing an actual patch based on it. If you think it is I could prepare one. -- nosy: +zuo ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Jan Kaliszewski z...@chopin.edu.pl added the comment: PS. Newer, shorter version: http://dpaste.org/2aiQ/ -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Éric Araujo mer...@netwok.org added the comment: Thanks for working on this. I have some remarks: 1) Please post diff files here instead of using external sites. See http://docs.python.org/devguide/patch#preparation 2) The license you chose doesn’t allow the PSF to include it into Python, see http://www.python.org/psf/contrib/contrib-form/ 3) abc looks like a module name, here something like NamedTupleABC or simply NamedTuple would be better. Note that this bug is only about type checking, the alternate form of defining named tuples thanks to this ABC is an additional feature; Raymond may reject it. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Changes by Éric Araujo mer...@netwok.org: -- dependencies: +Enhance Object/structseq.c to match namedtuple and tuple api ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Éric Araujo mer...@netwok.org added the comment: To make sure I understand: StructSeq is the C-pendent of named tuples, and a NamedTuple ABC would have to work with StructSeqs too? -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Raymond Hettinger rhettin...@users.sourceforge.net added the comment: Am deferring this to Py3.3. There is a workaround available just using duck-typing and I would like to wait until more more has been done on StructSeq before setting committing to an new namedtuple Abstract Base Class (one released, it would be very hard to change). Also, I would like to see how pprint() evolves (because it may also want to have special handling for named tuples). -- priority: high - low resolution: - later versions: +Python 3.3 -Python 2.7, Python 3.2 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Changes by Raymond Hettinger rhettin...@users.sourceforge.net: -- priority: normal - high ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Raymond Hettinger rhettin...@users.sourceforge.net added the comment: Discussed this with GvR. Here's a recap: For 2.6 and 3.1 which are already released, check for the _fields attribute. This is a guaranteed part of the API is not fragile. For the C structures, check for the n_fields attribute. In the future, the C API needs to grow to match the namedtuple() API and we should add an abstract base class describing the common API. Then you'll be able to write: if isinstance(obj, abc_namedtuple). -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Sridhar Ratnakumar sridh...@activestate.com added the comment: For more discussion on this, see http://stackoverflow.com/questions/2166818 -- components: +Library (Lib) nosy: +srid ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
New submission from Peter Waller peter.wal...@gmail.com: Apologies if there is a way of doing this, but I haven't been able to find anything. I need to be able to do the following: my_tuple = namedtuple(my_tuple, a b c) obj = my_tuple(1,2,3) if isinstance(obj, namedtuple): .. do stuff .. The best I could come up with for the moment is: if isinstance(obj, tuple) and type(obj) is not tuple: .. do stuff .. -- messages: 98437 nosy: pwaller severity: normal status: open title: No way to find out if an object is an instance of a namedtuple versions: Python 2.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Antoine Pitrou pit...@free.fr added the comment: In this case, what is wrong with: if isinstance(obj, my_tuple): ... or do you want to catch all namedtuples? And if so, why? (I suppose it would be easy to make all namedtuples inherit from a common base class, though) -- assignee: - rhettinger nosy: +pitrou, rhettinger priority: - normal stage: - needs patch type: - feature request versions: +Python 2.7, Python 3.2 -Python 2.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Éric Araujo mer...@netwok.org added the comment: Hello namedtuple is a factory function, not a class (as hinted by the naming in lower case, see PEP 8), so there are no instances of namedtuple. You can workaround that with issubclass, or checking for special namedtuples attributes (_asdict, _replace, but this is fragile), or just not use class cheking, often unneeded in Python code. Kind regards -- nosy: +Merwok ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Peter Waller peter.wal...@gmail.com added the comment: Hi Antoine and Eric, Thanks for your responses. I need to be able to catch all named tuples, I don't know in advance what instance of a namedtuple it might be, but I want my function to only accept named tuples. Is this unreasonable? (I am actually writing a C interface which does conversion from namedtuples to a form of RecordSpecification to do with databases. The namedtuple is arbitrarily created by the user.) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Antoine Pitrou pit...@free.fr added the comment: I need to be able to catch all named tuples, I don't know in advance what instance of a namedtuple it might be, but I want my function to only accept named tuples. Is this unreasonable? I don't know. Why exactly don't you want to accept something else? Perhaps the user has their own namedtuple-like class. Or perhaps they're fine with plain tuples. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Peter Waller peter.wal...@gmail.com added the comment: In this case, I need to have names for each object. It is also desirable to use the namedtuple because of their lightweight nature. If they were to subclass the namedtuple I would be happy to accept that, but I really do want them to pass some form of namedtuple to me, to have some sort of consistency. The named tuple gives an obvious simple interface for determining which fields are available and the order they are in. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Changes by betatim beta...@gmail.com: -- nosy: +thead ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Amaury Forgeot d'Arc amaur...@gmail.com added the comment: It seems a perfect case for duck typing style of programming: All namedtuple classes: - inherit from tuple - have a _fields class attribute These two properties could be the duck test for namedtuples, regardless of the actual implementation. -- nosy: +amaury.forgeotdarc ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7796] No way to find out if an object is an instance of a namedtuple
Peter Waller peter.wal...@gmail.com added the comment: Hi Amaury, Thanks. I had heard of but never bothered to read about duck-typing before now; though I have used it passively before. I think it does make sense in this case. I can't imagine any case where checking for the _fields attribute would fail and isinstance(x, namedtuple) would not. Besides which, for my current project I am forced to implement such a workaround anyway, so it doesn't affect me as such. The only reason that remains why I would want it is that I often use isinstance(x, Y) to deal with different Ys, and that was the thing I intuitively wanted to use in this case as a python programmer for quite a few years now. This is probably a pretty weak reason, so I am happy to close this issue if the consensus points to duck typing. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7796 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com