472,352 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

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 3687
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign...
3
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of...
2
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should...
2
by: Shark | last post by:
Hi, if we need to change the behavior of operator new, it is called overriding or overloading? My other question is, if we change the behavior of...
2
by: linq936 | last post by:
Hi, I have this piece code, struct TriStr { MyString str1; MyString str2; MyString str3; TriStr(MyString s1, MyString s2, MyString s3){...
6
by: johnmmcparland | last post by:
Hi all, when I write a subclass, can I inherit its superclass' << operator? As an example say we have two classes, Person and Employee. ...
14
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class...
1
by: Tony Johansson | last post by:
Hello! If I define operator == and != is then nessesary to override method Equals and GetHashCode that is inherited from System.Object ? If...
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.