python - 函数调用运算符优先级

标签 python function parsing expression

根据 this site函数调用优先于让我感到困惑的属性访问。

>>> class cls:
    def func(self):
        print("Method")


>>> def func():
    print("Global Func")


>>> obj=cls() 
>>> obj.func() #1
Method
>>> (obj.func)() #2
Method
>>> obj.(func()) #3
SyntaxError: invalid syntax

3 是预期的语法错误,但如果函数调用优先于属性访问,为什么 1 和 2 会产生相同的结果?

最佳答案

根据它链接到的官方 Python 文档,该网站略有错误:https://docs.python.org/3/reference/expressions.html#operator-precedence

您会注意到 Python 文档中列出的运算符优先级在表中的倒数第二个位置都在同一行:x[index], x[ index:index], x(arguments...), x.attribute.正如文档所述,

Operators in the same box have the same precedence. ... Operators in the same box group left to right (except for exponentiation, which groups from right to left).

无论出于何种原因,您链接到的网站都说相同,只是将这四样东西按顺序排列,而不是放在同一个框中。

关于python - 函数调用运算符优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57245810/

相关文章:

python - 语法错误 : non-default argument follows default argument

java - 我被 Java 作业“解析字符串”困住了

c# - MTOM - 在 VS 2010 中读取时遇到无效的 MIME 内容类型 header

python - CentOS 上 pip 包管理器的问题

python - python 中的定义 foo(*, a=1) 如何匹配文档引用中的正式 'argument list'?

python - PackageNotFoundError : Packages missing in current channels:

C 传递给要处理的数组的参数

php - 这是过滤数据和防止 SQL 注入(inject)和其他攻击的安全方法吗?

函数返回一个节点的所有子节点的表

xml - Perl XML::简单的 XMLout 函数