Advertisement

reversed() doesn't seem to work. (Python Tutorial)

Started by January 19, 2004 10:48 PM
9 comments, last by Drevay 20 years, 10 months ago
So I'm reading through the Python Tutorial (on chapter 5 now) and am so far thoroughly enjoying myself. I've run into a bit of a hitch, though. A peice of example code showed this:
>>> for i in reversed(xrange(1,10,2)):
...   print i
...  
and it should work just peachy. But I get this error when I try to run it through:
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'reversed' is not defined  
Is reversed() perhaps in a module of some sort that I have to import? Any help would be much appreciated. [edited by - Drevay on January 19, 2004 12:06:31 AM]
_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.
reversed() is in Python 2.4.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Advertisement
Python 2.4 isn''t even out yet, and this is the Python 2.3.3 tutorial I''m on.

You think they''d put in something that would only work in a future release? I doubt it.

_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.
I mean, sort() says the same thing if you don''t use it properly, but I can''t seem to get help on reversed().

I tried help(list.reversed) but it seems to come up with nothing.

What module would the reversed() function be from?

Perhaps it is in 2.4 only... Odd..

_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.
def reversed(x):   return x[::-1] 


[edited by - Fruny on January 19, 2004 12:59:23 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks, Fruny. Although I was wondering since the Tutorial treated it like a built in function ("This is how you blah blah blah"). It listed enumeration and iteritems along with it.

:S

_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.
Advertisement
quote: Original post by Drevay
So I''m reading through the Python Tutorial (on chapter 5 now) and am so far thoroughly enjoying myself.

I''ve run into a bit of a hitch, though. A peice of example code showed this:
>>> for i in reversed(xrange(1,10,2)):...   print i...   

and it should work just peachy.

Where did you get that code from? I don''t see it anywhere in the Python Tutorial. reversed() is due to be added as a built-in, as described in PEP 322.
Oh, well it is - I downloaded Python a few days ago, it''s in chapter 5, look there.

Odd that they''d put that in there...

Well, now I''m satisfied.

That was bugging the hell out of me!

_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.
Very useful note from Python tutorial (scroll up a line).

[Edit: So useful I made it a clicky!]

[edited by - Oluseyi on January 20, 2004 1:49:46 PM]
Note that the overhead of calling Fruny''s reversed function can cause a performance degradation. FAQ.

This topic is closed to new replies.

Advertisement