using compare operators for own classes 
Author Message
 using compare operators for own classes

This question has been asked some days ago by a colleague of mine,
perhaps a little bit ambiguous.

We think of using different compare operators (<,>,==) with our self
defined
classes. Of course this is possible on principle
overwriting the __cmp__ method of the class.

But we have to detect which concrete operator has been used !!

We do not want to call a cmp-method on some member variables of our
class
object directly. Instead we have to construct a global list containing
the
instance itself, the concrete type of compare operator and the
comparison value.

This list will be used later to generate code for a real-time hardware
platform.

We also thought of 'misusing' exception handling in the __cmp__ method,
using the
infos of the traceback. But we didn't find the correct source line.

==========================================================
list = []

class SIGNAL:
  def __cmp__(self, value):
     used_operator = '????'   ## this is our problem !!!
     list.append((self, used_operator, value))

test = SIGNAL()
print list

test < 10
print list
==========================================================

Any ideas ?

Thanks for any help in advance,
Jobst

----------------------------------------------------------------------
Dipl.-Ing. Jobst Richert     Tel:(+49) 5251-1638-649 or -1638686
dSPACE GmbH                  Fax:(+49) 5251-66529

D-33100 Paderborn (Germany)  WWW:www.dspace.de and/or
                                 www.dspaceinc.com
----------------------------------------------------------------------



Sun, 28 May 2000 03:00:00 GMT  
 using compare operators for own classes

Quote:

> This question has been asked some days ago by a colleague of mine,
> perhaps a little bit ambiguous.

> We think of using different compare operators (<,>,==) with our self
> defined
> classes. Of course this is possible on principle
> overwriting the __cmp__ method of the class.

> But we have to detect which concrete operator has been used !!

You have managed to highlight the only serious complaint I still have
with the python language (which I haven't managed to talk Guido into
fixing ;-)

In order to do the sort of operations you want, Python needs to treat
its comparision operators exactly the way that it treats its arithmetic
ones.

There are three changes required to permit this:

1) Python internals can no longer expect total orderings (easy fix :-)

2) a < b must be able to return arbitrary Python Object (not just 1|0)

3) Special methods need to be provided that allow user-defined classes
to overload the different comparision operators differently.  My
personal suggestion would be to allow:

__compare__(other, op)

where op in ['<', '>', '==', ...]

If a class defined __compare__, it would be used instead of __cmp__ to
implement comparision functions.

I'm currently aware of five projects that this would benefit greatly:

1) Numeric Python (we've been asking for this for over 2 years now)

2) Mathematica-like symbolic algebra in Python

3) Differential Equation extensions (I think this is at BeamTech)

4) Fuzzy numbers in Python

5) The work you mention here

Not only would this change add functionality to the language, but I
think that it would make the language more elegant by increasing the
symmetry among the binary operators.

My two cents worth - Jim



Sun, 28 May 2000 03:00:00 GMT  
 using compare operators for own classes

Quote:
> You have managed to highlight the only serious complaint I still have
> with the Python language (which I haven't managed to talk Guido into
> fixing ;-)

> In order to do the sort of operations you want, Python needs to treat
> its comparision operators exactly the way that it treats its arithmetic
> ones.

I agree that it needs to be fixed -- however it will be a lot of
work.  If enough people join the PSA or if and when the Python
Consortium materializes, I will be able to work on this.

--Guido van Rossum (home page: http://www.python.org/~guido/)



Sun, 28 May 2000 03:00:00 GMT  
 using compare operators for own classes

Quote:
> __compare__(other, op)

Yuck. (and there should be a self argument first, of course).
Try one of:

 * __binop__(self, other, op) as a generic mechanism for implementing
   generalised operators (alied to a class datum listing the binary
   operators valid as arguments, to give the compiler some chance at
   efficiency).  If you're going to take an op argument, why treat
   comparisons specially ?  But this is still ugly.

 * __less__(self, other), __more__(self, other), __same__(self, other).
   Some care would be needed over situations where __less__(a,b) isn't
   the same as __more__(b,a) (which is sometimes valid) but this would
   be the natural extension of the model followed by addition and
   multiplication.

(By the way, do the latter behave `how I want' when they aren't Abelian,
ie when a+b != b+a or a*b != b*a ?)

   I don't really believe in <=, >=, !=, <> and >< as separate entities:
   they are negations of >, < and == variously, though it would make
   sense for them to catch a standard `Incomparable' error from the
   operator being negated and treat it as (after negation) success.  The
   notion `less than or equal to' can be done as `a<b or a==b' so I
   prefer to have `a<=b' denote `a>b fails or is false'.

Why the desire to have comparisons return non-booleans ?
I guess I could just about comprehend wanting `a < b' to be None if
false but a-b otherwise, or similar.  But it'd wreck a < b < c, which is
a really nice feature in my book.  (Wouldn't it ?)

Quote:
> 1) Python internals can no longer expect total orderings (easy fix :-)

Oh good.

As for

Quote:
> Not only would this change add functionality to the language

never forget what Antoine de Saint-Exupery said:

``Perfection (in design) is achieved not when there is nothing more to
add, but rather when there is nothing more to take away.''

Quote:
> My two cents worth - Jim

and my tuppence,

        Eddy.



Sun, 28 May 2000 03:00:00 GMT  
 using compare operators for own classes

Quote:
> We think of using different compare operators (<,>,==) with our self
> defined
> classes. Of course this is possible on principle
> overwriting the __cmp__ method of the class.

> But we have to detect which concrete operator has been used !!

Sorry, no way. All comparison operators are translated to a single
comparison call, and the information about the type of comparison is
available only in the bytecode following the call. There has been some
discussion about this problem in the Matrix-SIG, but there is no
solution at the moment.
--
-------------------------------------------------------------------------------

Laboratoire de Dynamique Moleculaire   | Tel.: +33-4.76.88.99.28
Institut de Biologie Structurale       | Fax:  +33-4.76.88.54.94
41, av. des Martyrs                    | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France         | Nederlands/Francais
-------------------------------------------------------------------------------


Mon, 29 May 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. using compare operators for own classes

2. Using boolean operators with string compare function

3. Using __repr__ or __str__ for own printable class?

4. Comparing class instance with class

5. Writting the Own class

6. Adding Your Own Class to a Multi Dll App

7. Errors with application builder and own ActiveX class

8. Problem: import own Java classes in Jython

9. modifying classes used by other classes

10. Comparing 2 class types

11. Comparing user-defined classes

12. How do C++ and Smalltalk Classes compare?

 

 
Powered by phpBB® Forum Software