Lisp mentality vs. Python mentality

"Martin v. Löwis" martin at v.loewis.de
Sat Apr 25 04:30:56 EDT 2009


>>>     Ok... Then what's pythonic? Please give a pythonic implementation...
>> Use the builtin a==b, similar to (equal a b)
> 
>     But how about extensibility?

== is extensible. To compare two things for equality, use ==.

This is what Carl Banks referred in his original posting. You just
*don't* implement a function that compares two lists, not even as
an exercise for estetic, optimal, and useful implementations -
because you'll know that it won't be useful, anyway, if you can already
use the builtin == in the first place.

I see that you allow for a different comparison function. I do wonder
what the use case for this is - in what application do you have to
compare two lists for equality, and the item's __eq__ is inappropriate?
What would break if you fix the item's __eq__, instead of writing
your own comparison algorithm?

If I would ever run into such a case, I would inline the loop, rather
than writing a function that receives a callable:

  for index, v1 in enumerate(l1):
      if not comp(v1, l2[index]):
          res = False
          break
  else:
      res = True

Regards,
Martin



More information about the Python-list mailing list