[英]Why do some methods of built-in types operate in-place and some don't?
dog = 'penny'
print(dog.title())
dog_names = ['pete', 'luke', 'shane']
print(dog_names.remove('shane'))
Why does Python return an output of Penny
for dog.title()
but None
for dog_names.remove('shane')
?为什么 Python 为dog.title()
返回Penny
的输出,而为dog_names.remove('shane')
返回None
? Why can I not print the list dog_name
with the method remove
while I can use the method title
on dog
?为什么我不能使用方法remove
打印列表dog_name
而我可以在dog
上使用方法title
?
I understand that I get None
because dog_name.remove
has no return, but how does dog.title
have a return?我知道我得到None
因为dog_name.remove
没有回报,但是dog.title
如何有回报?
The title()
function is a pre-defined function in Python which is used to covert the first character of the string into uppercase and the remaining characters into lowercase and return a new string. title()
函数是 Python 中的一个预定义函数,用于将字符串的第一个字符转换为大写,将其余字符转换为小写,并返回一个新字符串。 in your example if you run print(dog)
you can see that penny
is all lowercase, but if you run print(dog.title())
you can see that the first letter in Penny
which is P
is uppercase and the remaining is lowercase在你的例子中,如果你运行print(dog)
你可以看到penny
都是小写的,但是如果你运行print(dog.title())
你可以看到Penny
中的第一个字母P
是大写的,其余的都是小写的
In order to answer your question, first, you need to understand what's being returned.为了回答您的问题,首先,您需要了解返回的内容。
When calling print
on a function or method, it will display the returned value.在函数或方法上调用print
时,它将显示返回值。 With that being said, you can check the return value of both functions from the documentation话虽如此,您可以从文档中检查两个函数的返回值
https://docs.python.org/2/tutorial/datastructures.html#more-on-lists https://docs.python.org/2/tutorial/datastructures.html#more-on-lists
list.remove(x) Remove the first item from the list whose value is x. list.remove(x) 从列表中删除值为 x 的第一项。 It is an error if there is no such item.如果没有这样的项目,这是一个错误。
https://docs.python.org/3.7/library/stdtypes.html#str.title https://docs.python.org/3.7/library/stdtypes.html#str.title
str.title() Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase. str.title()返回字符串的标题版本,其中单词以大写字符开头,其余字符为小写。
title()
returns the titled case string, that's why it's printed.注意title()
如何返回带标题的 case 字符串,这就是它被打印的原因。remove()
doesn't return anything, in this case, python has a default return value of None
( Source ) that's why it's printed when calling remove()
另一方面, remove()
不返回任何内容,在这种情况下,python 的默认返回值为None
( Source ) 这就是为什么在调用remove()
时会打印它A function
/ method
in any programming language performs a specific task.任何编程语言中的function
/ method
执行特定任务。 A task could be performed on the inputs passed to the function.可以对传递给函数的输入执行任务。 After performing the task, the function may want to give the result back to the caller of the function.执行任务后,函数可能希望将结果返回给函数的调用者。 It may do that by:它可以通过以下方式做到这一点:
remove()
method of list data type) (例如list数据类型的remove()
方法)title()
method) ( title()
方法)Your remove() method doesn't return anything back to the caller.您的remove()方法不会将任何内容返回给调用者。 Instead it just deletes an element from the list.相反,它只是从列表中删除一个元素。 To print the list after removing an element:要在删除元素后打印列表:
dog_names.remove('shane')
print(dog_names)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.