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


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

Re: __future__ and __rdiv__

Started byTerry Reedy <tjreedy@udel.edu>
First post2012-01-23 03:07 -0500
Last post2012-01-23 08:39 +0000
Articles 2 — 2 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: __future__ and __rdiv__ Terry Reedy <tjreedy@udel.edu> - 2012-01-23 03:07 -0500
    Re: __future__ and __rdiv__ Duncan Booth <duncan.booth@invalid.invalid> - 2012-01-23 08:39 +0000

#19250 — Re: __future__ and __rdiv__

FromTerry Reedy <tjreedy@udel.edu>
Date2012-01-23 03:07 -0500
SubjectRe: __future__ and __rdiv__
Message-ID<mailman.4955.1327306073.27778.python-list@python.org>
On 1/23/2012 12:22 AM, Massimo Di Pierro wrote:
> 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:

If you want to get 2 rather than 2.0 after uncommenting, then I believe 
you should use // and __floordiv__. In fact, you do not even need the 
future import. But if you mean for Number to be like a float rather than 
int, do as you are (with / and __truediv__).

Terry Jan Reedy

[toc] | [next] | [standalone]


#19253

FromDuncan Booth <duncan.booth@invalid.invalid>
Date2012-01-23 08:39 +0000
Message-ID<Xns9FE35809BB799duncanbooth@127.0.0.1>
In reply to#19250
Terry Reedy <tjreedy@udel.edu> wrote:

> But if you mean for Number to be like a float
> rather than int, do as you are (with / and __truediv__).
> 
Or even __rtruediv__


-- 
Duncan Booth http://kupuguy.blogspot.com

[toc] | [prev] | [standalone]


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


csiph-web