[Edu-sig] Reversing dictionaries, closures, permutations etc.
Kirby Urner
urnerk at qwest.net
Fri Jan 23 22:18:28 EST 2004
> OTOH I believe in math you talk about the inverse function (e.g. tan
> <-> atan) and not the reverse function. And my car has a reverse. :-)
>
Yes, that further tips the scales in favor of inverse.
> > So is 2.4 mylist.reversed() something that returns a list,
> > vs. reverses it in place? Is there a parallel mylist.sorted()?
>
> No. reversed() and sorted() are builtins; reversed() returns an
> iterator that iterates over a sequence in reverse; sorted() returns a
> new list that is sorted:
>
So dir([]) will not show 'sorted' as a list method? I should just go read
the PEP.
I have to keep reminding myself that Python ain't SmallTalk, and we have
built-in top-level functions that don't show up anywhere in the object
hierarchy as class or instance methods -- that bit me before when I tried to
see int(x) always in terms of some x.__int__() method.
E.g.
>>> int('123')
123
But:
>>> '123'.__int__()
Traceback (most recent call last):
File "<pyshell#88>", line 1, in -toplevel-
'123'.__int__()
AttributeError: 'str' object has no attribute '__int__'
> >>> for i in reversed([1,2,3]): print i
>
So that wouldn't extend to generators (which can be iterated over, but "in
reverse" would make no sense). Again, I need to read the PEP. Out of date
over here.
Looking forward to 2.4
Thanks for the preview!
Kirby
More information about the Edu-sig
mailing list