469,283 Members | 2,178 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

How do I learn operator overriding?

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?

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

Jul 18 '05 #1
6 3631
On Fri, 19 Sep 2003 14:25:23 -0400, pytho wrote:
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?


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.

--
luc wastiaux
email: luc-at-4002-dot-org
jabber: lu*@jabber.4002.org

Jul 18 '05 #2
py****@sarcastic-horse.com wrote:
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:
class Date: .... 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()
.... Date <class __main__.Date at 0x402e053c> q1 = Date(2001, 1)
q4 = Date(2001, 4)
q4 - q1

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.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ You and I / We've seen it all / Chasing our hearts' desire
\__/ The Russian and Florence, _Chess_
Jul 18 '05 #3

<py****@sarcastic-horse.com> wrote in message
news:ma**********************************@python.o rg...
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
Jul 18 '05 #4

<py****@sarcastic-horse.com> wrote in message
news:ma**********************************@python.o rg...
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.

Jul 18 '05 #5
John Roth wrote:
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.)

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ You and I / We've seen it all / Chasing our hearts' desire
\__/ The Russian and Florence, _Chess_
Jul 18 '05 #6

"Erik Max Francis" <ma*@alcyone.com> wrote in message
news:3F**************@alcyone.com...
John Roth wrote:
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.)


I did say *infection*.

John Roth
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ You and I / We've seen it all / Chasing our hearts' desire
\__/ The Russian and Florence, _Chess_

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Edward Diener | last post: by
3 posts views Thread by Cheng Mo | last post: by
2 posts views Thread by Shark | last post: by
2 posts views Thread by linq936 | last post: by
6 posts views Thread by johnmmcparland | last post: by
14 posts views Thread by Hunk | last post: by
1 post views Thread by Tony Johansson | last post: by
19 posts views Thread by C++Liliput | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.