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

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

问题描述

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

print "这个字符串现在将是大写的".upper()

...为什么upper"后面有一对空括号?它有什么作用吗?有没有一种情况,人们会在那里放东西?谢谢!

解决方案

括号表示要调用的方法

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

如果你只是说upper,那么它返回的是一个方法,而不是你在应用该方法时得到的值

<预><代码>>>>打印这个字符串现在将是大写的".upper<0x7ff41585fe40处str对象的内置方法上层>>>>

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() returns the value of the method applied to the string

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天全站免登陆