相关疑难解决方法(0)

访问文字属性适用于所有类型,但不适用于`int`; 为什么?

我已经读过python中的所有东西都是一个对象,因此我开始尝试不同的类型并调用__str__它们 - 起初我感到非常兴奋,但后来我感到困惑.

>>> "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
Run Code Online (Sandbox Code Playgroud)
  • 为什么something.__str__()除了"一切"之外还要工作int
  • 123不是一个对象类型的int

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

52
推荐指数
4
解决办法
2376
查看次数

标签 统计

language-lawyer ×1

python ×1

python-2.7 ×1

python-3.x ×1