Python3 int.__sizeof__() 产生语法错误

标签 python oop integer

<分区>

以下代码产生语法错误。有谁知道为什么在使用对象属性时会出现语法错误?

Python 3.7.4 (default, Jul 27 2020, 09:35:23) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>1.__sizeof__()
  File &quot;&lt;stdin
    1.__sizeof__()
               ^
SyntaxError: invalid syntax
>>>

最佳答案

The lexer is greedy ,因此它将 1.__sizeof__() 识别为 float 文字 1. 后跟标识符 __sizeof__,而不是int 字面量后跟 . 运算符。

$ python -m tokenize <<< "1.__sizeof__()"
1,0-1,2:            NUMBER         '1.'
1,2-1,12:           NAME           '__sizeof__'
1,12-1,13:          OP             '('
1,13-1,14:          OP             ')'
1,14-1,15:          NEWLINE        '\n'
2,0-2,0:            ENDMARKER      ''

使用括号override协助词法分析器:

>>> (1).__sizeof__()
28

关于Python3 int.__sizeof__() 产生语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64935152/

相关文章:

python - 如何连接字符串计数和 Int

css - webkit 只是切断了 css 编号中的小数点……如何解决?

python - 如何缩进多行字符串的内容?

python - 你如何计算数据帧中的这种差异?

design-patterns - 我应该使用什么设计模式进行导入/导出?

javascript - 保护 "private"类成员免遭修改

char 作为低范围数字类型?

python - 远程 ipython 内核不显示图

python - 将单张图像与 10 张或更多图像进行比较并找到匹配的图像

PHP OOP 入门