[Tutor] Overloading comparisons

Hugo González Monteverde hugonz-lists at h-lab.net
Tue Nov 15 19:22:33 CET 2005


Hi all,

I defined an object wich describes a video clip, like this

class VideoSegment:
     def __init__(self, filename):
         # Attributes that have to be present
         self.filename = filename


The thing is, I will define an array of these objects for a cache, and 
would like to keep that array having less than 1000 elements. 
Periodically I will check it, and remove the oldest element.

suppose I have the modification time as an argument.

self.time = os.stat(self.filename).st_mtime

I can define the following for sorting the array:

     def __cmp__(self, other):
         """polymorph for comparisons"""
         if self.time < other.time:
             return -1
         elif self.time > other.time:
             return 1
         else:
             return 0

But then I want to test for existance doing

if 'lala.avi' in myarray:

and that has to be done by name. Is there any way this could work? My 
current __cmp__ of course treats objects with the same timestamp as 
identical objects.

Any comments are greatly appreciates, maybe my OOP is just sloppy...

Hugo


More information about the Tutor mailing list