Python方法名称末尾的括号是什么? [英] What are the parentheses for at the end of Python method names?

查看:226
本文介绍了Python方法名称末尾的括号是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般是Python和编程的新手.现在,我很难理解内置或用户创建的方法名称末尾的空括号的功能.例如,如果我写:

I'm a beginner to Python and programming in general. Right now, I'm having trouble understanding the function of empty parentheses at the end of method names, built-in or user-created. For example, if I write:

print "This string will now be uppercase".upper()

...为什么在上"之后有一对空括号?它有什么作用吗?是否存在一种情况,有人会在其中放东西?谢谢!

...why is there an empty pair of parentheses after "upper?" Does it do anything? Is there a situation in which one would put something in there? Thanks!

推荐答案

括号表示您要调用该方法

the parentheses indicate that you want to call the method

upper()返回应用于字符串的方法的值

upper() returns the value of the method applied to the string

如果您只说upper,则它将返回一个方法,而不是应用该方法时获得的值

if you simply say upper, then it returns a method, not the value you get when the method is applied

>>> print "This string will now be uppercase".upper
<built-in method upper of str object at 0x7ff41585fe40>
>>> 

这篇关于Python方法名称末尾的括号是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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