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.
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
Changes by Eric Snow ericsnowcurren...@gmail.com:
--
nosy: +eric.snow
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7796
___
___
Python-bugs-list
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
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
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
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
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
Changes by Santoso Wijaya santoso.wij...@gmail.com:
--
nosy: +santa4nt
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7796
___
___
Python-bugs-list
Changes by Sridhar Ratnakumar sridh...@activestate.com:
--
nosy: -srid
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7796
___
___
Changes by Daniel Urban urban.dani...@gmail.com:
--
nosy: +durban
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7796
___
___
Python-bugs-list
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
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
___
É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,
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
___
É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
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
Changes by Raymond Hettinger rhettin...@users.sourceforge.net:
--
priority: normal - high
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7796
___
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
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
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 ..
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: -
É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,
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?
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
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
Changes by betatim beta...@gmail.com:
--
nosy: +thead
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7796
___
___
Python-bugs-list mailing list
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
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
29 matches
Mail list logo