Different Random Numbers Between Already/Not Yet Compiled Code

Jason Orendorff jason at jorendorff.com
Thu Jan 24 16:37:14 EST 2002


Eric Eide wrote:
> Tim Peters wrote:
> > Eric Eide wrote:
> > > Sorting the returned `items' list before iterating 
> > > over it solved my unpredictability problem,
> >
> > A good solution -- unless you're merely sorting by the keys'
> > memory addresses (and then this problem will come up 
> > again, sooner or later).
> 
> No, at least I knew not to do that :-).

Unless you've defined __cmp__() that's what you're doing.  :-)

>>> class Foo:
...     def __init__(self, name):
...         self.name = name
...

>>> x = map(Foo, ['x', 'y', 'jason', 'alligator', 'picnic'])
>>> x.sort()
>>> for i in x:
...     print i.name   #Note that they're not sorted by name...
...
picnic
y
x
alligator
jason
>>> for i in x:
...     print i  # Note that they're sorted by address.
...
<__main__.Foo instance at 0x00913BE0>
<__main__.Foo instance at 0x00915380>
<__main__.Foo instance at 0x00917D30>
<__main__.Foo instance at 0x00926548>
<__main__.Foo instance at 0x0092FE70>

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list