4
這不起作用。爲什麼在整數週圍需要括號來調用它的方法?
>>> 10.__str__()
File "<stdin>", line 1
10.__str__()
^
SyntaxError: invalid syntax
但這個工程。
>>> (10).__str__()
'10'
爲什麼爲了調用其方法需要在整數左右的括號?列表或其他數據類型似乎並不需要它。
>>> [1, 2].__str__()
'[1, 2]'
>>> {'a': 'foo'}.__str__()
"{'a': 'foo'}"
非常有趣:'10 ..__ str __()'。 – Chris
@Chris:因爲'10.'給出了10.0,這是一個浮動對象。所以'10 ..__ str __()== 10.0 .__ str __()'。 –
@KevinGuan,是的,我打算以此爲例,而不是要求澄清。 – Chris