Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19249 > unrolled thread

__future__ and __rdiv__

Started byMassimo Di Pierro <massimo.dipierro@gmail.com>
First post2012-01-22 23:22 -0600
Last post2012-01-22 23:22 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  __future__ and __rdiv__ Massimo Di Pierro <massimo.dipierro@gmail.com> - 2012-01-22 23:22 -0600

#19249 — __future__ and __rdiv__

FromMassimo Di Pierro <massimo.dipierro@gmail.com>
Date2012-01-22 23:22 -0600
Subject__future__ and __rdiv__
Message-ID<mailman.4950.1327296175.27778.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web