为什么在调用方法时需要将文字括在括号中? [英] Why do I need to enclose a literal in parentheses while invoking a method?

查看:40
本文介绍了为什么在调用方法时需要将文字括在括号中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

42.__abs__()
  File "<ipython-input-43-9690433aefe5>", line 1
    42.__abs__()
             ^
SyntaxError: invalid syntax

带 ()

(42).__abs__()
42

这里需要 () 的原因是什么?

What is the reason for () being necessary here?

推荐答案

42. 将被解析器解释为浮点 42.0.因此,. 未被识别为实例与其方法之一之间的连接器.Python 正确地抱怨在浮点数之后直接有一些变量名,因为这是无效的语法.

42. will be interpreted by the parser as a float 42.0. Ergo the . is not recognized as the connector between an instance and one of its methods. Python rightfully complains that there is some variable name directly after a float, since that is invalid syntax.

这篇关于为什么在调用方法时需要将文字括在括号中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆