On Sat, 30 Jul 2005 23:37:04 +1000, Steven D'Aprano <[EMAIL PROTECTED]> wrote:
>On Sat, 30 Jul 2005 14:20:50 +0200, tiissa wrote:
>
>> Steven D'Aprano wrote:
>>> Playing around with comparisons of functions (don't ask), I discovered an
>>> interesting bit of unintuitive behaviour:
>>>
>>>>>>a = lambda y: y
>>>>>>b = lambda y: y
>>>>>>a
>>> <function <lambda> at 0xf70598ec>
>>>>>>b
>>> <function <lambda> at 0xf7059844>
>>>>>>a < b
>>> False
>>>
>>> So I'm puzzled about how Python compares the two.
>>
>> Seems to me the object addresses are compared in this case. But I'm too
>> lazy to check it in the source. ;)
>
>Strangely enough, I'm lazy enough to not check the source too :-)
>
>Actually, more to the point, I don't read C, so even if I did check the
>source, I wouldn't know what it was trying to tell me.
>
>> However, the doc [1] warns you about such comparisons: """Most other
>> types compare unequal unless they are the same object; the choice
>> whether one object is considered smaller or larger than another one is
>> made arbitrarily but consistently within one execution of a program."""
>
>I am aware of that. That's a wart.
>
What if rich sorting "measured" the objects it was sorting, to form a
measurement tuple
of primitive types, so that you'd wind up sorting as if by a decorated list
with the
measurement tuple as the decoration, e.g., (an I think having to wrap complex
for sorting
is CWrap ;-)
Just a sketch to illustrate an idea for sorting...
----< richlysorted.py >-------------------------------------
def richlysorted(seq):
return ((type(v) is CWrap and [v.v] or [v])[0]
for d,v in sorted(measure_dec(seq)))
INT = LONG = FLOAT = 0
COMPLEX = 1
STR = 2
UNICODE = 3
FUNCTION = 4
TYPE = 5
DEFAULT = 6
type_sort_order = dict(
int=INT, long=LONG, float=FLOAT,
complex=COMPLEX,
str=STR, unicode=UNICODE,
function=FUNCTION,
type=TYPE)
class CWrap(object):
def __init__(self, v): self.v=v
def __eq__(self, other): return True
def measure_dec(seq):
for v in seq:
sort_priority = type_sort_order.get(type(v).__name__, DEFAULT)
if sort_priority == COMPLEX:
yield (sort_priority, v.real, v.imag), CWrap(v)
elif sort_priority <= UNICODE:
yield (sort_priority,), v
elif sort_priority == FUNCTION:
yield (sort_priority, v.func_name, v.func_code.co_argcount), v
elif sort_priority == TYPE:
yield (sort_priority, v.__name__), v
else:
yield (sort_priority, type(v).__name__, repr(v)) , v
def test():
class C: pass
class D(object): pass
def foo(): pass
def bar(a,b): pass
tbs = [1, -1.0, 1+0j, 2j, lambda x:x, test, measure_dec, lambda:None,
foo, bar, D, D(), C(), C, 2**32, -2**32]
for tup in zip(tbs, measure_dec(tbs)):
print '%35s | %-s' % tup
print '%s\nSorted:'% (50*'-')
for item in richlysorted(tbs):
print '%35s' % item
if __name__ == '__main__':
test()
------------------------------------------------------------
[16:57] C:\pywk\ut>py24 richlysorted.py
1 | ((0,), 1)
-1.0 | ((0,), -1.0)
(1+0j) | ((1, 1.0, 0.0), <__main__.CWrap object at
0x02F00B2C>)
2j | ((1, 0.0, 2.0), <__main__.CWrap object at
0x02F00B4C>)
<function <lambda> at 0x02EE8DF4> | ((4, '<lambda>', 1), <function <lambda>
at 0x02EE8DF4>)
<function test at 0x02EE8D4C> | ((4, 'test', 0), <function test at
0x02EE8D4C>)
<function measure_dec at 0x02EE8CA4> | ((4, 'measure_dec', 1), <function
measure_dec at 0x02EE8C
A4>)
<function <lambda> at 0x02EE8E2C> | ((4, '<lambda>', 0), <function <lambda>
at 0x02EE8E2C>)
<function foo at 0x02EE8D84> | ((4, 'foo', 0), <function foo at
0x02EE8D84>)
<function bar at 0x02EE8DBC> | ((4, 'bar', 2), <function bar at
0x02EE8DBC>)
<class '__main__.D'> | ((5, 'D'), <class '__main__.D'>)
<__main__.D object at 0x02F0044C> | ((6, 'D', '<__main__.D object at
0x02F0044C>'), <__main__.
D object at 0x02F0044C>)
<__main__.C instance at 0x02F004CC> | ((6, 'instance', '<__main__.C instance at
0x02F004CC>'), <
__main__.C instance at 0x02F004CC>)
__main__.C | ((6, 'classobj', '<class __main__.C at
0x02EE78CC>'), <cla
ss __main__.C at 0x02EE78CC>)
4294967296 | ((0,), 4294967296L)
-4294967296 | ((0,), -4294967296L)
--------------------------------------------------
Sorted:
-4294967296
-1.0
1
4294967296
2j
(1+0j)
<function <lambda> at 0x02EE8E2C>
<function <lambda> at 0x02EE8DF4>
<function bar at 0x02EE8DBC>
<function foo at 0x02EE8D84>
<function measure_dec at 0x02EE8CA4>
<function test at 0x02EE8D4C>
<class '__main__.D'>
<__main__.D object at 0x02F0044C>
__main__.C
<__main__.C instance at 0x02F004CC>
Regards,
Bengt Richter
--
http://mail.python.org/mailman/listinfo/python-list