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" besidesint
? - is
123
not object of typeint
?
you need parens:
(4).__str__()
the problem lexer thinks "4." going floating-point number.
also, works:
x = 4 x.__str__()
Comments
Post a Comment