[IronPython] Named Tuple and IronPython 2

Michael Foord fuzzyman at voidspace.org.uk
Fri Sep 19 14:26:54 CEST 2008


Hello all,

At PyCon UK Raymond Hettinger showed off the Named Tuple; a very useful 
recipe for creating tuples with named fields. It is becoming part of the 
standard library in Python 2.6.

http://code.activestate.com/recipes/500261/

 >>> from namedtuple import namedtuple
 >>> thing = namedtuple('thing', ('whizz', 'pop'))
 >>> thing
<class '__builtin__.thing'>
 >>> i = thing(1, 2)
 >>> i.whizz
1
 >>> i.pop
2
 >>> w, p = i
 >>> w, p
(1, 2)
 >>> i
thing(whizz=1, pop=2)
 >>>

I've attached a modified version that works with IronPython 2 B5 (it 
doesn't work with 2B4 due to a very odd bug that is now fixed).

The only change needed was to comment out the use of 'sys._getframe' 
which is there for pickle support.

It is only conditionally used, because Jython didn't support this either 
- but on Jython _getframe wasn't defined at all so the conditional check 
fails on IronPython:

    # Bypass this step in enviroments where
    # sys._getframe is not defined (Jython for example).
    if hasattr(_sys, '_getframe'):
        result.__module__ = _sys._getframe(1).f_globals['__name__']

I'll suggest a modification to the recipe that works with IronPython. A 
better solution would be to implement '_getframe' of course... :-)

Michael Foord

-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/
http://www.trypython.org/
http://www.ironpython.info/
http://www.resolverhacks.net/
http://www.theotherdelia.co.uk/

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: namedtuple.py
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080919/58edb54c/attachment.ksh>


More information about the Ironpython-users mailing list