List operator overloading snag

Discussion in 'Python' started by Steven Brent, Apr 25, 2004.

  1. Steven Brent

    Steven Brent Guest

    Dear Group:

    I am a little perplexed by a basic example from the O'Reilly book, 2nd ed.,
    which demonstrates overloading some builtin list operators by embedding
    lists in a wrapper class.

    Below is a snippet which shows the wrapper class and the overloaded
    operator definition within (in this case, '__or__')

    ## BEGIN SNIPPET (from setwrapper.py)

    class Set:
    def __init__(self, value = []):
    self.data = []
    self.concat(value)

    def union(self, other):
    res = self.data[:]
    for x in other:
    if not x in res:
    res.append(x)
    print 'using Set.__or__' #for debugging
    return Set(res)

    def __repr__(self):
    return 'Set: ' + `self.data` #for debugging

    def __or__(self, other):
    return self.union(other)

    ## END SNIPPET

    The problem is that I can't seem to get my wrapped lists to use the
    overloaded versions of the operators without calling the customize
    instance method directly, as shown below:
    >>> import setwrapper; from setwrapper import *


    >>> S1 = Set(['s','p','a','m'])
    >>> S2 = Set(['s','c','a','t'])


    >>> print S1 or S2

    Set: ['s', 'p', 'a', 'm'] # custom __repr__ used but still default
    # __or__ behavior, returns first true operand

    >>> print S1.__or__(S2)

    using Set.__or__
    Set: ['s', 'p', 'a', 'm', 'c', 't'] # now all the custom behaviors

    This happens regardless of whether the module is imported into IDLE, or run
    standalone. Obviously I'm missing something very basic namespace issue, and
    I don't want to gloss over any bumps in my learning curve.

    Many thanks.

    --
    **********************
    www.emptydrum.com
    ....like a think tank
    without the thinking
    or the tank
    **********************
     
    Steven Brent, Apr 25, 2004
    #1
    1. Advertising

  2. Steven Brent

    Paul Prescod Guest

    Steven Brent wrote:

    >...
    > The problem is that I can't seem to get my wrapped lists to use the
    > overloaded versions of the operators without calling the customize
    > instance method directly, as shown below:
    >
    >>>>import setwrapper; from setwrapper import *

    >
    >
    >>>>S1 = Set(['s','p','a','m'])
    >>>>S2 = Set(['s','c','a','t'])

    >
    >
    >>>>print S1 or S2


    Try this:

    >>> print S1 | S2


    Paul Prescod
     
    Paul Prescod, Apr 25, 2004
    #2
    1. Advertising

  3. Steven Brent wrote:
    > Dear Group:
    >
    > I am a little perplexed by a basic example from the O'Reilly book, 2nd ed.,
    > which demonstrates overloading some builtin list operators by embedding
    > lists in a wrapper class.
    >
    > Below is a snippet which shows the wrapper class and the overloaded
    > operator definition within (in this case, '__or__')
    >

    <code for class Set with __or__() method snipped>
    >
    >
    > The problem is that I can't seem to get my wrapped lists to use the
    > overloaded versions of the operators without calling the customize
    > instance method directly, as shown below:
    >
    >>>>import setwrapper; from setwrapper import *

    >
    >
    >>>>S1 = Set(['s','p','a','m'])
    >>>>S2 = Set(['s','c','a','t'])

    >
    >
    >>>>print S1 or S2

    >
    > Set: ['s', 'p', 'a', 'm'] # custom __repr__ used but still default
    > # __or__ behavior, returns first true operand
    >
    >
    >>>>print S1.__or__(S2)

    >
    > using Set.__or__
    > Set: ['s', 'p', 'a', 'm', 'c', 't'] # now all the custom behaviors
    >
    > This happens regardless of whether the module is imported into IDLE, or run
    > standalone. Obviously I'm missing something very basic namespace issue, and
    > I don't want to gloss over any bumps in my learning curve.
    >
    > Many thanks.
    >


    Try S1 | S2. The '|' operator (commonly known as the bitwise 'or'
    operator for integers) is what gets customized. You cannot change the
    meaning of the Python boolean 'or' which returns the left side value if
    it is true, else it returns the right side value. So what you're seeing
    is normal behavior, you always get the left side value since it's truth
    value is true. See also __nonzero__() in the Python docs.

    Btw, if you really want sets, you can use builtin module sets (new in 2.3).

    --
    Shalabh
     
    Shalabh Chaturvedi, Apr 25, 2004
    #3
  4. Steven Brent

    Peter Otten Guest

    Steven Brent wrote:

    >>>> print S1 or S2

    > Set: ['s', 'p', 'a', 'm'] # custom __repr__ used but still default
    > # __or__ behavior, returns first true operand
    >
    >>>> print S1.__or__(S2)

    > using Set.__or__
    > Set: ['s', 'p', 'a', 'm', 'c', 't'] # now all the custom behaviors


    Try S1 | S2 instead, __or__() somewhat misleadingly implements the | aka
    "bitwise or" operator.

    Peter
     
    Peter Otten, Apr 25, 2004
    #4
  5. Steven Brent

    Steven Brent Guest

    Crikey, is *that* all it was!?
    Thanks so much to all who responded so quickly.
    Y'all made my day :)

    --SB
     
    Steven Brent, Apr 25, 2004
    #5
    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. Richard
    Replies:
    40
    Views:
    1,370
    Randy Webb
    Dec 19, 2004
  2. John Smith
    Replies:
    2
    Views:
    423
    Ivan Vecerina
    Oct 6, 2004
  3. Replies:
    11
    Views:
    737
    James Kanze
    May 16, 2007
  4. hurcan solter
    Replies:
    3
    Views:
    733
    Cholo Lennon
    Aug 29, 2007
  5. Richard

    One fixed....new snag to contend with

    Richard, Dec 17, 2004, in forum: Javascript
    Replies:
    33
    Views:
    288
    Beauregard T. Shagnasty
    Dec 19, 2004
Loading...

Share This Page