473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

compare classes and not class instances

I not only want to compare class *instances* but also the classes
themselves. Something like:

class T(object):
@classmethod
def __cmp__(cls, other):
return cmp(cls.__name__, other.__name__)
@instancemethod
def __cmp__(self, other):
return cmp(self.x, other.x)

class B(T): pass
class A(T): pass

sorted([B, A]) => [A, B]

My motivation for doing so is simply to sort classes based on their
names and not (as it seems is the default behaviour) on the order in
which they were created.

I guess I could always just do something like sorted(classes,
key=lambda cls: cls.__name__)...but where's the fun in that? :-)

Mar 2 '06 #1
2 2601
ja****@gmail.com wrote:
I not only want to compare class *instances* but also the classes
themselves. Something like: sorted([B, A]) => [A, B]

My motivation for doing so is simply to sort classes based on their
names and not (as it seems is the default behaviour) on the order in
which they were created.

I guess I could always just do something like sorted(classes,
key=lambda cls: cls.__name__)...but where's the fun in that? :-)


A class is just an instance of its metaclass. Therefore the metaclass is the
right place to implement the __cmp__() method:
class T: .... class __metaclass__(type):
.... def __cmp__(cls, other):
.... return cmp(cls.__name__, other.__name__)
.... class B(T): pass .... class A(T): pass .... class C(T): pass .... sorted([C, B, A])

[<class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>]

Peter
Mar 2 '06 #2
Awesome! Perfect!

Thanks
Janto

Mar 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

145
by: David MacQuigg | last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating classes in Python. I'm trying to figure out what fundamental benefit there is to having classes. Is all this complexity...
2
by: | last post by:
I have this class ------------- class Component { /*class*/ Data *d; /*class*/ Draw *a; }; ------------- from "Component" derive classes like "TextBox", "Button", "Label", "ComboBox" etc from...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
4
by: JohnR | last post by:
Assume you have a grocery store application. An order consists of an order header with the "customer" info, and multiple "item" records (one for each thing they ordered). Now, in a regular DB...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
0
by: Jim Stools | last post by:
Hopefully this will make some sense.. I have a database that has around 50 tables - I thought about putting each table in a class and the data connection in a class then I could manage the (tables)...
4
by: Jim Stools | last post by:
Forget the re-post I had my clock set 12 hours earlier Hopefully this will make some sense.. I have a database that has around 50 tables - I thought about putting each table in a class and the...
18
by: Donn Ingle | last post by:
Hi, I'm getting myself really confused. I have three classes. Two of them need to reference the third, like so: Canvas ---Stack <--- Thing I am doing this right now: s = Stack()
2
by: Avi | last post by:
Hi all, I have two instances of the same class. Is it possible to compare private members of two classes and to come with a list of differences? Thanks, Avi
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.