How do I learn operator overriding?

P

python

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:
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?

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


Thanks for the help.
 
L

luc wastiaux

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:

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?

in your class:

def __sub__(self, mydate):
# perform self - mydate
# and return a new qdate object


I can't reach www.python.org but there should be some documentation on
operator overloading. Also, I recommend the book "learning Python" from
O'reilly ( http://www.oreilly.com/catalog/lpython/ ) which covers most of
the things you need to know about python.
 
E

Erik Max Francis

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?

Just override the __sub__ method:
.... def __init__(self, year, quarter):
.... self.year = year
.... self.quarter = quarter
.... def quarters(self):
.... return self.year*4 + self.quarter
.... def __sub__(self, other):
.... return self.quarters() - other.quarters()
....
3

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

Yep. I've gotten 262 MB since 2 am this morning. Another worm is out
and about, I'd presume.
 
T

Terry Reedy

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?

Look up 3.3 Special method names & 3.3.6 Emulating numeric types in
ref manual.
And, on a completely unrelated note, I am getting a truly amazing amount
of spam today. Anyone else?

As in over 1000 fake Microsoft Updates with virus attachments?

Terry J. Reedy
 
J

John Roth

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:

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
 
E

Erik Max Francis

John said:
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.

Being completely safe from infection doesn't mean that you won't get
hammered by the spam the worms generate. All you need to be is in the
address book of someone whose machine was compromised. (My machines
don't run Windows, so they certainly haven't bee compromised by a
Windows worm, but I'm seeing tremendous spam loads nevertheless.)
 
J

John Roth

Erik Max Francis said:
Being completely safe from infection doesn't mean that you won't get
hammered by the spam the worms generate. All you need to be is in the
address book of someone whose machine was compromised. (My machines
don't run Windows, so they certainly haven't bee compromised by a
Windows worm, but I'm seeing tremendous spam loads nevertheless.)

I did say *infection*.

John Roth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top