osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Explanation of list reference


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