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__() 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -