How do I learn operator overriding?

Discussion in 'Python' started by python@sarcastic-horse.com, Sep 19, 2003.

  1. Guest

    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.
    , Sep 19, 2003
    #1
    1. Advertising

  2. luc wastiaux Guest

    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: luc@jabber.4002.org
    luc wastiaux, Sep 19, 2003
    #2
    1. Advertising

  3. 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 && && 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_
    Erik Max Francis, Sep 19, 2003
    #3
  4. Terry Reedy Guest

    <> wrote in message
    news:...
    > 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
    Terry Reedy, Sep 19, 2003
    #4
  5. John Roth Guest

    <> wrote in message
    news:...
    > 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.
    >
    John Roth, Sep 19, 2003
    #5
  6. 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 && && 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_
    Erik Max Francis, Sep 19, 2003
    #6
  7. John Roth Guest

    "Erik Max Francis" <> wrote in message
    news:...
    > 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 && && 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_
    John Roth, Sep 19, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cheng Mo
    Replies:
    3
    Views:
    696
    Victor Bazarov
    Nov 19, 2004
  2. Replies:
    1
    Views:
    329
    Erik Max Francis
    Sep 19, 2003
  3. Porky Pig Jr
    Replies:
    3
    Views:
    1,011
    Fuzzyman
    May 12, 2004
  4. CoreyWhite
    Replies:
    1
    Views:
    633
    JohnQ
    Mar 31, 2007
  5. Alexander
    Replies:
    20
    Views:
    1,018
    BGB / cr88192
    Sep 11, 2010
Loading...

Share This Page