Monday, 15 September 2014

python - Accessing attributes on literals work on all types, but not `int`; why? -



python - Accessing attributes on literals work on all types, but not `int`; why? -

i have read in python object, , such started experiment different types , invoking __str__ on them — @ first feeling excited, got confused.

>>> "hello world".__str__() 'hello world' >>> [].__str__() '[]' >>> 3.14.__str__() '3.14' >>> 3..__str__() '3.0' >>> 123.__str__() file "<stdin>", line 1 123.__str__() ^ syntaxerror: invalid syntax why something.__str__() work "everything" besides int? is 123 not object of type int?

you need parens:

(4).__str__()

the problem lexer thinks "4." going floating-point number.

also, works:

x = 4 x.__str__()

python python-2.7 python-3.x language-lawyer

No comments:

Post a Comment