operator overloading
Emile van Sebille
emile at fenx.com
Mon Jun 5 19:38:36 EDT 2000
More information about the Python-list mailing list
Mon Jun 5 19:38:36 EDT 2000
- Previous message (by thread): OSCON help?
- Next message (by thread): operator overloading
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Ken, You missed my point, while your point is taken. My point, if I understood the question, was how could one overload the less than operator. In absence of an example, I took that to mean how could one change the behavior of the less than symbol. Which needs to happen in a class definition, which then allows the class to decide what less than means. So I provided an example that showed changed behavior for less than. ;-) Of course, only the class can know how two instances should compare, and it should then return an appropriate value. That the example should have fully shown that return values can be negative, zero, or positive, indicating less than, equals, and greater than respectively, is of course my fault. What-if-a-six-turned-out-to-be-nine-ly y'rs Emile van Sebille emile at fenx.com ------------------- ----- Original Message ----- From: Ken Seehof Newsgroups: comp.lang.python To: python-list at python.org Sent: Monday, June 05, 2000 3:02 PM Subject: Re: operator overloading Emile van Sebille wrote: Here's a quick example of using __cmp__ >>> class Test: def __init__(self,a): self.a = a def __cmp__(self,other): return self.a < other.a >>> a = Test(3) >>> b = Test(4) >>> print a<b 0 >>> print a>b 1 HTH, Emile van Sebille emile at fenx.com ------------------- Um. No. Not exactly. Usually 1 designates "true" and 0 designates "false" unless I'm mistaken. Also IMHO, > means "greater than" and < means "less than". BTW there is yet another comparison operator == than means "equal to". :-) The return value of __cmp__ should be -1, 0, 1 for <, ==, >, respectively. The following more closely conforms to these conventions: >>> class Test: ... def __init__(self,a): ... self.a = a ... def __cmp__(self,other): ... if self.a == other.a: ... return 0 ... elif self.a < other.a: ... return -1 ... else: ... return 1 ... >>> a = Test(1) >>> b = Test(2) >>> c = Test(2) >>> a<b, a>b, a==b, a!=b (1, 0, 0, 1) >>> b==c, b<c, b!=c (1, 0, 0) -- Ken Seehof kens at sightreader.com ----- Original Message ----- From: <kazan at kazan.fenx.com> Newsgroups: comp.lang.python To: <python-list at python.org> Sent: Monday, June 05, 2000 1:01 PM Subject: operator overloading > Hello > > How can I overload the less than operator? > learning python did mention something about __cmp__ but did > not provide any examples. > > > Thanks in advance > > Erling > -- -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-list/attachments/20000605/789d3c55/attachment.html>
- Previous message (by thread): OSCON help?
- Next message (by thread): operator overloading
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list