Int methods (was RE: string.py)

Tim Peters tim_one at email.msn.com
Sun Feb 20 17:54:55 EST 2000


[Moshe Zadka]
> I think Gerrit is on to something. How about....(drum roll)...int
> methods?

More than a year ago, I got Guido's OK to add some methods to longs -- there
are a pile of things (like "how many bits does this long consume?") wrt
longs that are intolerably inefficient to compute in Python, but for years
nothing got done about that partly because we didn't want to bloat the
builtins with specialty functions.  Now nothing gets done about that
entirely because nobody has time to do it <0.3 wink>.

> ...
> I see there might be parser problems, but maybe (5).radix? Python seems
> willing to check for it:
>
> >>> a = (5).radix(2)
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> AttributeError: 'int' object has no attribute 'radix'

The parser has trouble with 5.radix because "maximal munch" lexing sucks up
"5." as a float.  Note this cute one, though:

>>> 5..radix
Traceback (innermost last):
  File "<pyshell#1>", line 1, in ?
    5..radix
AttributeError: 'float' object has no attribute 'radix'
>>>

That is, that's already parsed as (5.).float.

There's little reason to apply methods to numeric literals, though, and
wrapping in parens is always sufficient.  Note too:

>>> (sys).stdin
<PyShell.PyShell instance at 80b260>
>>>

That may look strange at first, but the LHS of an attribute fetch can
already be any expression whatsoever.

no-problems-here-except-time-ly y'rs  - tim






More information about the Python-list mailing list