ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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":
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...
__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
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.