The short answer is that the proper way to call it with sort_key() is sorted([x, a, b], key=lambda i: i.sort_key()). default_sort_key() is just a shortcut to this lambda expression. You shouldn't use sort_key() unless you want use a nonstandard order (which is broken right now anyway).
Aaron Meurer On Wed, Jun 15, 2011 at 12:49 PM, Mateusz Paprocki <[email protected]> wrote: > Hi, > > On 15 June 2011 19:56, smichr <[email protected]> wrote: >> >> What is the rationale for the differences in these two keys: >> >> >>> sorted([x, a, b], key=default_sort_key) >> [a, b, x] >> >>> sorted([x, a, b], key=Basic.sort_key) >> [x, a, b] > > In [1]: default_sort_key(x) > Out[1]: ((2, 0, Symbol), (1, (x,)), ((1, 0, Number), (0, ()), (), 1), 1) > In [2]: Basic.sort_key(x) > Out[2]: ((2, 0, Symbol), (0, ()), ((1, 0, Number), (0, ()), (), 1), 1) > In [3]: Expr.sort_key(x) > Out[3]: ((2, 0, Symbol), (1, (x,)), ((1, 0, Number), (0, ()), (), 1), 1) > In [4]: Atom.sort_key(x) > Out[4]: ((2, 0, Symbol), (1, (x,)), ((1, 0, Number), (0, ()), (), 1), 1) > sort_key() is an instance method, so if you call it like this, you override > method resolution algorithm and use wrong method for the type. Also note > that default_sort_key uses sympify() on its argument (irrelevant in this > case). [3] and [4] give the same result because Expr.sort_key() contains > redundant implementation for atoms. > >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sympy" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/sympy?hl=en. >> > > Mateusz > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/sympy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
