访问文字的属性适用于所有类型,但不适用于`int`;为什么? [英] Accessing attributes on literals work on all types, but not `int`; why?
问题描述
我已经阅读到python中的所有内容都是一个对象,因此我开始尝试不同的类型并对它们调用 __str__
—起初我感到非常兴奋,但后来我感到困惑.
I have read that everything in python is an object, and as such I started to experiment with different types and invoking __str__
on them — at first I was feeling really excited, but then I 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
- 为什么
something.__str__()
除了int
之外还适用于一切"? -
123
不是类型为int
的对象吗? - Why does
something.__str__()
work for "everything" besidesint
? - Is
123
not an object of typeint
?
推荐答案
所以您认为您可以 dance 浮点数?
123
和3.14
一样是一个对象,问题"位于语言的语法规则之内;解析器认为我们将要定义 float —不是带有跟踪方法调用的 int .
So you think you can dance floating-point?
123
is just as much of an object as 3.14
, the "problem" lies within the grammar rules of the language; the parser thinks we are about to define a float — not an int with a trailing method call.
如果将数字用括号括起来,我们将获得预期的行为,如下所示.
We will get the expected behavior if we wrap the number in parenthesis, as in the below.
>>> (123).__str__()
'123'
或者如果我们只是在 123
之后添加一些空格:
Or if we simply add some whitespace after 123
:
>>> 123 .__str__()
'123'
不适用于123.__str__()
的原因是 123
之后的 dot 被解释为小数点一些部分声明的 floating-point .
The reason it does not work for 123.__str__()
is that the dot following the 123
is interpreted as the decimal-point of some partially declared floating-point.
>>> 123.__str__()
File "", line 1
123.__str__()
^
SyntaxError: invalid syntax
解析器尝试将__str__()
解释为数字序列,但显然失败—.并且我们得到一个 SyntaxError ,基本上是说解析器偶然发现了一些意外的东西.
The parser tries to interpret __str__()
as a sequence of digits, but obviously fails — and we get a SyntaxError basically saying that the parser stumbled upon something that it did not expect.
查看123.__str__()
时,python解析器可以使用 3 个字符,并将这些 3 个字符解释为 integer ,或,它可以使用 4 个字符并将它们解释为浮点的开始.
When looking at 123.__str__()
the python parser could use either 3 characters and interpret these 3 characters as an integer, or it could use 4 characters and interpret these as the start of a floating-point.
123.__str__()
^^^ - int
123.__str__()
^^^^- start of floating-point
就像一个小孩想要在盘子上放尽可能多的蛋糕一样,解析器很贪婪,并且想一次吞下尽可能多的东西—即使这并不总是最好的主意,因此选择了后者(更好").
Just as a little child would like as much cake as possible on their plate, the parser is greedy and would like to swallow as much as it can all at once — even if this isn't always the best of ideas —as such the latter ("better") alternative is chosen.
当后来意识到__str__()
绝不能解释为浮点数的小数位时,已经为时已晚. SyntaxError .
When it later realizes that __str__()
can in no way be interpreted as the decimals of a floating-point it is already too late; SyntaxError.
注意
Note
123 .__str__() # works fine
在上面的代码段中,由于没有 number 可以包含空格,因此必须将123
(请注意空格)解释为整数.这意味着它在语义上等同于(123).__str__()
.
In the above snippet, 123
(note the space) must be interpreted as an integer since no number can contain spaces. This means that it is semantically equivalent to (123).__str__()
.
注意
Note
123..__str__() # works fine
上面的方法也有效,因为一个数字最多可以包含一个小数点,这意味着它等同于(123.).__str__()
.
The above also works because a number can contain at most one decimal-point, meaning that it is equivalent to (123.).__str__()
.
本节包含相关文字的词汇定义.
This section contains the lexical definition of the relevant literals.
floatnumber ::= pointfloat | exponentfloat
pointfloat ::= [intpart] fraction | intpart "."
exponentfloat ::= (intpart | pointfloat) exponent
intpart ::= digit+
fraction ::= "." digit+
exponent ::= ("e" | "E") ["+" | "-"] digit+
integer ::= decimalinteger | octinteger | hexinteger | bininteger
decimalinteger ::= nonzerodigit digit* | "0"+
nonzerodigit ::= "1"..."9"
digit ::= "0"..."9"
octinteger ::= "0" ("o" | "O") octdigit+
hexinteger ::= "0" ("x" | "X") hexdigit+
bininteger ::= "0" ("b" | "B") bindigit+
octdigit ::= "0"..."7"
hexdigit ::= digit | "a"..."f" | "A"..."F"
bindigit ::= "0" | "1"
这篇关于访问文字的属性适用于所有类型,但不适用于`int`;为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!