473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 3732
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 method, and provide that for end-users of my class,...
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 template cannot be static.The compiler says cannot...
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 explain my problem in the simplest way). I have...
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 operator new, do we use malloc to do that or we use...
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){ this->str1 = s1;
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. class Person has the << operator; /**
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 from which two classes (normal char string and a...
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 yes will Equals have the same functionalitty as...
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 need to create a map of my string (say a class...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.