How do I learn operator overriding?

John Roth newsgroups at
Fri Sep 19 21:43:03 CEST 2003

<python at> wrote in message
news:mailman.1063995987.26144.python-list at
> Hi-
> I need to make a class for quartlerly dates.  I need to be able to compare
> two quarterly dates and get the number of quarters between them.  For
> example:
> >>> 2001q1 = qDate(year=2001, quarter=1)
> >>> 2001q4 = qDate(year=2001, quarter=4)
> >>> 2001q4 - 2001q1
> 3
> The only problem is that I have no idea how to override operators in
> python.  Can anyone give me a few trivial examples of how it is done?

Operators are special methods that start and end with two underscores.
Look in either the library or the language manual; you'll find all you need
to know there. For example, generic compares use the __cmp__()
method. There are also extended compares.

> And, on a completely unrelated note, I am getting a truly amazing amount
> of spam today.  Anyone else?

A new mass mailer worm hit the net at about 9:00 PM EST last night
(at least, that's when I started getting the spam from it.) It seems to be
using a really old hole in Windows, meaning that if you've kept up to date
with your patches, you should be safe from infection. At least, that's
the information I have to date. I put my "junk" folder on auto delete
to protect my mailbox, and that's it.

Also of note is that one of the addresses in the header *isn't* spoofed,
so there's a clear back track to the infected machines. I haven't
verified this myself, though.

John Roth
> Thanks for the help.

More information about the Python-list mailing list