Commutative object in emulating numbers

Discussion in 'Python' started by iu2, Sep 14, 2009.

  1. iu2

    iu2 Guest

    Hi,

    I reached the chapter "Emulating numeric types" in the python
    documentation and I tried this:

    >>> class A:

    def __mul__(self, a):
    return 'A' * a

    Now, this works as expected:
    >>> a = A()
    >>> a * 3

    'AAA'

    But this doesn't (also as expected):
    >>> 3 * a


    Traceback (most recent call last):
    File "<pyshell#45>", line 1, in <module>
    3 * a
    TypeError: unsupported operand type(s) for *: 'int' and 'instance'
    >>>


    What do I need to do in order to make the two classes, int and A,
    commutative?
    (In the same way that string and int are commutative over "*")
    Thanks
    iu2, Sep 14, 2009
    #1
    1. Advertising

  2. iu2

    iu2 Guest

    On Sep 14, 7:52 am, iu2 <> wrote:
    > Hi,
    >
    > I reached the chapter "Emulating numeric types" in the python
    > documentation and I tried this:
    >
    > >>> class A:

    >
    >         def __mul__(self, a):
    >                 return 'A' * a
    >
    > Now, this works as expected:>>> a = A()
    > >>> a * 3

    >
    > 'AAA'
    >
    > But this doesn't (also as expected):
    >
    > >>> 3 * a

    >
    > Traceback (most recent call last):
    >   File "<pyshell#45>", line 1, in <module>
    >     3 * a
    > TypeError: unsupported operand type(s) for *: 'int' and 'instance'
    >
    >
    >
    > What do I need to do in order to make the two classes, int and A,
    > commutative?
    > (In the same way that string and int are commutative over "*")
    > Thanks


    By commutative I mean give the same result, that is
    3 * a
    will also return 'AAA'
    iu2, Sep 14, 2009
    #2
    1. Advertising

  3. iu2

    Chris Rebert Guest

    On Sun, Sep 13, 2009 at 9:52 PM, iu2 <> wrote:
    > Hi,
    >
    > I reached the chapter "Emulating numeric types" in the python
    > documentation and I tried this:
    >
    >>>> class A:

    >        def __mul__(self, a):
    >                return 'A' * a
    >
    > Now, this works as expected:
    >>>> a = A()
    >>>> a * 3

    > 'AAA'
    >
    > But this doesn't (also as expected):
    >>>> 3 * a

    >
    > Traceback (most recent call last):
    >  File "<pyshell#45>", line 1, in <module>
    >    3 * a
    > TypeError: unsupported operand type(s) for *: 'int' and 'instance'
    >>>>

    >
    > What do I need to do in order to make the two classes, int and A,
    > commutative?


    You need to define __rmul__():
    http://docs.python.org/dev/3.0/reference/datamodel.html#object.__rmul__

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Sep 14, 2009
    #3
  4. iu2

    iu2 Guest

    On Sep 14, 8:16 am, Chris Rebert <> wrote:
    > On Sun, Sep 13, 2009 at 9:52 PM, iu2 <> wrote:
    > > Hi,

    >
    > > I reached the chapter "Emulating numeric types" in the python
    > > documentation and I tried this:

    >
    > >>>> class A:

    > >        def __mul__(self, a):
    > >                return 'A' * a

    >
    > > Now, this works as expected:
    > >>>> a = A()
    > >>>> a * 3

    > > 'AAA'

    >
    > > But this doesn't (also as expected):
    > >>>> 3 * a

    >
    > > Traceback (most recent call last):
    > >  File "<pyshell#45>", line 1, in <module>
    > >    3 * a
    > > TypeError: unsupported operand type(s) for *: 'int' and 'instance'

    >
    > > What do I need to do in order to make the two classes, int and A,
    > > commutative?

    >
    > You need to define __rmul__():http://docs.python.org/dev/3.0/reference/datamodel.html#object.__rmul__
    >
    > Cheers,
    > Chris
    > --http://blog.rebertia.com- Hide quoted text -
    >
    > - Show quoted text -


    Thanks!
    iu2, Sep 14, 2009
    #4
  5. On Sun, 13 Sep 2009 21:52:26 -0700, iu2 wrote:

    > Hi,
    >
    > I reached the chapter "Emulating numeric types" in the python
    > documentation and I tried this:

    [...]
    > What do I need to do in order to make the two classes, int and A,
    > commutative?



    Try adding a __rmul__ method:


    class A:
    def __mul__(self, a):
    return 'A' * a
    __rmul__ = __mul__



    --
    Steven
    Steven D'Aprano, Sep 14, 2009
    #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. Replies:
    3
    Views:
    617
    Gary Thorpe
    Sep 1, 2005
  2. Mike Orb

    Emulating variables static to the page

    Mike Orb, Sep 2, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    393
    Mike Orb
    Sep 2, 2005
  3. Ben Fidge

    Emulating referrers?

    Ben Fidge, Dec 14, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,596
    Kevin Spencer
    Dec 14, 2005
  4. Lance Hoffmeyer
    Replies:
    2
    Views:
    513
    Lance Hoffmeyer
    Jul 26, 2007
  5. J Krugman

    "Commutative" regexps?

    J Krugman, May 28, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    76
    Greg Bacon
    May 28, 2004
Loading...

Share This Page