PEP239 (Rational Numbers) Reference Implementation and new issues

James J. Besemer jb at cascade-sys.com
Thu Oct 10 00:26:10 EDT 2002


Bengt Richter wrote:

>Why not a way to introduce special rules for a limited scope? E.g., if there
>were a 'scope' keyword that would introduce in indented block where the rules
>would apply, and a dictionary defining rules and hooks. Then you could write
>
>    scope rational_rules:
>        a = b/c         # the '/' operator coerces to rational, let's say
>        blah()          # blah's internal rules would not be affected
>    d = a		# would have to act according to enclosing scope
>
>rational_rules might be {'enter_scope':__enter_rational__,
>                         'leave_scope':__leave_rational__,
>                         'exception_hook': __rational_exception__, ...}
>
>More than one set of rules is conceivable, as a sequence of dictionaries.
>  
>

This is an interesting idea but it strikes me as overly "inventive."  It 
would be a much bigger change to Python than simply adding rationals.  

>You could import stuff (e.g., from __future__) or whatever. 
>

If anything as dramatic as changing division to produce rational results 
is introduced I think it should apply globally to the entire module, not 
selectively per scope.

And I don't see any need for "special scope rules".  

In any context where rational numbers already exist, we can define "/" 
to work naturally.  That is any division involving one or more rational 
arguments would product a rational result.

Personally, I don't have a problem saying something like "rat( 3, 4 )" 
to introduce a new rational.  It's consistent with the rest of Python's 
OO.  

Alternatively, a "R" suffix to ordinary integers would be pretty clean, 
e.g., you could say "3/4R".

Personally, FWIW, I oppose changing builtins so that int(x)/int(y) 
produces a rational.  For the small minority who would use rationals, 
one of the above forms should be no hardship.

>You'd probably like
>
>    scope classic_division:
>        assert 3/4==0
>        ...
>
>for some stuff ?
>
I count myself among the people who did not want integer division to 
change from it's original definition.

However, given that it HAS changed, I don't think it should vary on a 
per-scope basis.  At least not beyond:

    assert int( 3/4 ) == 0
or
    assert 3//4 == 0

Regards

--jb

-- 
James J. Besemer		503-280-0838 voice
2727 NE Skidmore St.		503-280-0375 fax
Portland, Oregon 97211-6557	mailto:jb at cascade-sys.com
				http://cascade-sys.com	








More information about the Python-list mailing list