__future__ and __rdiv__

Discussion in 'Python' started by Massimo Di Pierro, Jan 23, 2012.

  1. Hello everybody,

    I hope somebody could help me with this problem. If this is not the right place to ask, please direct me to the right place and apologies.
    I am using Python 2.7 and I am writing some code I want to work on 3.x as well. The problem can be reproduced with this code:

    # from __future__ import division
    class Number(object):
    def __init__(self,number):
    self.number=number
    def __rdiv__(self,other):
    return other/self.number
    print 10/Number(5)

    It prints 2 as I expect. But if I uncomment the first line, I get:

    Traceback (most recent call last):
    File "test.py", line 8, in <module>
    print 10/Number(5)
    TypeError: unsupported operand type(s) for /: 'int' and 'Number'

    Is this a bug or the __future__ division in 3.x changed the way operators are overloaded? Where can I read more?

    Massimo
     
    Massimo Di Pierro, Jan 23, 2012
    #1
    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. Poor Yorick

    __future__ and unrecognised flags

    Poor Yorick, Dec 12, 2008, in forum: Python
    Replies:
    0
    Views:
    266
    Poor Yorick
    Dec 12, 2008
  2. Poor Yorick
    Replies:
    0
    Views:
    284
    Poor Yorick
    Dec 13, 2008
  3. Steve Holden
    Replies:
    1
    Views:
    401
    Rhamphoryncus
    Dec 14, 2008
  4. Karl Chen

    timeit and __future__

    Karl Chen, Jun 19, 2009, in forum: Python
    Replies:
    1
    Views:
    239
    Steven D'Aprano
    Jun 20, 2009
  5. Mattsteel
    Replies:
    4
    Views:
    382
    Mattsteel
    Jan 30, 2010
Loading...

Share This Page