LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2004, 03:58 PM   #1
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Python issue


Ok, i'm playing around with writing custom class comparison methods by implementing my own __cmp__ methods. I'm encountering a strange issue that i not entirely "getting":

class:
Code:
class Person:
    def __init__(self, name, *arg):
        self.name = name

    def __cmp__(self, other):
        print "comparing %s to %s = %s" % (self.name, other.name, self.name == other.name)
        return self.name == other.name

    def __repr__(self):
        return "Person:\n\tname: %s" % (self.name)
output:
Code:
>>> import comparing as c
>>> p1 = c.Person("John")
>>> p2 = c.Person("Bob")
>>> p1 == p2
comparing John to Bob = False
True
Why does the method return 'True' when the comparison (as is shown in the print string) is actually 'False'?


Last edited by coolman0stress; 05-10-2004 at 04:03 PM.
 
Old 05-10-2004, 04:05 PM   #2
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Ummm.... I could be wrong about this (because though I know Python, I am no guru), shouldn't those be intagers instead of strings? Like follows:

Code:
def __cmp__(self, other):
        print "comparing %s to %s = %d" % (self.name, other.name, int(self.name == other.name))
        return int(self.name == other.name)
Tell me if that works.
 
Old 05-10-2004, 04:09 PM   #3
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Original Poster
Rep: Reputation: 30
I tried that, but would give the same result.

Even tried this:
Code:
    def __cmp__(self, other):
        print "comparing %s to %s = %s" % (self.name, other.name, self.name == other.name)
        ret = int(self.name == other.name)
        print ret
        return ret
Output:
Code:
>>> p1 == p2
comparing John to Bob = False
0
True
There must be something i'm missing in my understanding of Python.
 
Old 05-10-2004, 04:33 PM   #4
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,692

Rep: Reputation: 45
could just do a 'p1 is p2' instead of p1 == p2, return blah
 
Old 05-10-2004, 06:21 PM   #5
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Original Poster
Rep: Reputation: 30
is would compare references, i'm actually trying to compare what's inside the objects. So even if the references are different, the data contained within the objects could be the same...
 
Old 05-11-2004, 02:40 AM   #6
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Quote:
__cmp__ (self, other)
Called by comparison operations if rich comparison (see above) is not defined. Should return a negative integer if self < other, zero if self == other, a positive integer if self > other. If no __cmp__(), __eq__() or __ne__() operation is defined, class instances are compared by object identity (``address''). See also the description of __hash__() for some important notes on creating objects which support custom comparison operations and are usable as dictionary keys. (Note: the restriction that exceptions are not propagated by __cmp__() has been removed since Python 1.5.)
With __cmp__, returning 0 means "are equal", returning something < 0 means "less than", and returning something > 0 means "greater than". So, returning self.name == other.name would return 0 for unequal names, but that would be interpreted as "are equal". You can easily just return self.name.__cmp__(other.name) just to make it consistent
 
Old 05-11-2004, 10:08 AM   #7
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Original Poster
Rep: Reputation: 30
Humm... that sort of makes sense.



Thanks Strike
 
Old 05-11-2004, 01:35 PM   #8
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Well, generally if you are just deferring to another method, I generally like to invoke it the same way. So if I've got a __str__ method that basically just returns the str representation of some member of that class, I'd do:

Code:
def __str__(self):
    return self.item.__str__()
I guess that comes as habit since when you subclass things and you want to invoke the parent class constructors, you do:

Code:
class Child(Parent):
    def __init__(self, *args, **kwargs):
        # stuff here
        Parent.__init__(self, args, kwargs)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 07:02 PM
installing python library's (Python Numeric) Four Linux - Newbie 1 10-16-2005 02:31 PM
Python guru's - Is this a python bug? or is it me? bardinjw Programming 2 06-23-2005 08:17 AM
WineX, python-gnome, and python-gtk DrD Fedora 0 08-03-2004 12:11 PM
install python 2.3 ,necssary to remove python 2.2 ngan_yine Linux - Newbie 7 12-28-2003 04:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration