python STL?

Dietrich Epp dietrich at zdome.net
Fri Nov 14 17:41:23 EST 2003


On Nov 14, 2003, at 1:07 PM, xam wrote:

> std::sort - works on any well-ordered object collection (you just 
> define
> your own operator< if you need to sort based on some weird criteria)

class Foo:
   def __init__(self, bar):
     self.bar = bar
   def __cmp__(self, other):
     return cmp(self.bar, other.bar)
   def __repr__(self):
     return 'Foo(%i)' % self.bar

myList = [Foo(1), Foo(7), Foo(3), Foo(8), Foo(-1755)]
myList.sort()
print myList

Yeilds:
[Foo(-1755), Foo(1), Foo(3), Foo(7), Foo(8)]

STL stands for Standard Template Library.  With a dynamic type system 
it's completely unnecessary -- you don't need a new type of list to 
sort a new type of object.

Personally, I hate the STL because it adds code without adding 
functionality and I avoid it at all costs when programming in C++.  
It's also not as portable as it should be.  I hope this doesn't spark a 
big thread.  Everyone programs in C++ for different reasons for 
different requirements.






More information about the Python-list mailing list