Explanation of list reference
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sun Feb 16 03:40:29 EST 2014
On Sun, 16 Feb 2014 10:08:22 +0200, Marko Rauhamaa wrote:
> Case in point, if everything is a reference, how come:
>
> >>> "hello".__str__()
> 'hello'
> >>> 1.__str__()
> SyntaxError: invalid syntax
Because it is a syntax error, just like the parser tells you. When the
parser sees "1." it expects a floating point number, and "1.__str__()" is
not a legal float.
There are three simple ways to get the effect that you want:
py> x = 1; x.__str__() # don't use a literal
'1'
py> (1).__str__() # parenthesize the literal
'1'
py> 1 .__str__() # offset it from the dot with a space
'1'
--
Steven
More information about the Python-list
mailing list