[Bryan] >>>> for example, i've noticed several java developers i know >>>> write python code like >>>> this: >>>> >>>> foo_list = [...] >>>> for i in range(len(foo_list)): >>>> print '%d %s' % (i, foo_list[i])
[Fredrik Lundh] >>> which is a perfectly valid way of doing things if you're targeting older >>> Python platforms as well (including Jython). [astyonax] >> But it's not the pythonic way. [Terry Reedy] > I don't think you understood what Fredrik said. It was the Python way > before enumerate() builtin was added and remains the Python way if you wish > to write for older versions of Python and Jython. On jython 2.1, I use something like this #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= try: enumerate except NameError: def enumerate(iterable): results = [] ; ix = 0 for item in iterable: results.append( (ix, item) ) ix = ix+1 return results if __name__ == "__main__": my_list = [0, 1, 1, 2, 3, 5, 8, ] for ix, fibo in enumerate(my_list): print "Position %d: %d" % (ix, fibo) #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Which runs like:- C:\alank>python -V Python 2.4.3 C:\alank>python fibo.py Position 0: 0 Position 1: 1 Position 2: 1 Position 3: 2 Position 4: 3 Position 5: 5 Position 6: 8 C:\alank>jython --version Jython 2.1 on java (JIT: null) C:\alank>jython fibo.py Position 0: 0 Position 1: 1 Position 2: 1 Position 3: 2 Position 4: 3 Position 5: 5 Position 6: 8 Of course, the efficiency is different across cpython vs. jython, but it's nice to have the same pythonic code running across both. And when jython progresses beyond 2.1, (any day now!), it will still work seamlessly. regards, -- alan kennedy ------------------------------------------------------ email alan: http://xhaus.com/contact/alan -- http://mail.python.org/mailman/listinfo/python-list