简体   繁体   English

为什么某些内置类型的方法可以就地操作,而有些则不能?

[英]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()返回字符串的标题版本,其中单词以大写字符开头,其余字符为小写。

  • Notice how title() returns the titled case string, that's why it's printed.注意title()如何返回带标题的 case 字符串,这就是它被打印的原因。
  • On the other hand 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:它可以通过以下方式做到这一点:

  1. By modifying the parameters passed to it or By altering the object on which method was called.通过修改传递给它的参数或通过更改调用方法的对象。 (For example, remove() method of list data type) (例如list数据类型的remove()方法)
  2. By returning the result to the caller.通过将结果返回给调用者。 ( title() method) title()方法)
  3. A combination of (1) and (2) (1)和(2)的组合

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.

相关问题 什么 python 内置 function 可以将一些 function 应用于列表中的每个元素? - What python built-in function can apply some function to every element of list in-place? 为什么有些方法使用点符号而其他方法没有? - Why do some methods use dot notation and others don't? 为什么熊猫 reindex() 不能就地运行? - Why doesn't pandas reindex() operate in-place? 为什么一些内置构造函数以小写字母开头? - Why do some built-in constructors begin with a lower case letter? 为什么有些内置的Python函数只有pass? - Why do some built-in Python functions only have pass? 某些PEP是否为内置序列类型保证了len(...)的O(1)复杂度? - Is O(1) complexity of len(…) guaranteed by some PEP for built-in sequence types? 为什么Python的内置集合类型的变异方法没有返回任何内容而不是集合本身? - Why do mutating methods for Python's built-in collection types return nothing instead of the collection itself? 为什么某些内置函数的语法错误消息有所不同? - Why syntax error messages for some built-in functions are different? 就地删除CSV的某些行 - Delete some rows of a CSV, in-place 为什么weakref 不支持Python 中的内置类型? - Why weakref doesn't support built-in types in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM