简体   繁体   English

.pop()方法如何仅通过作为另一个值来重新定义变量?

[英]How is the .pop() method able to redefine a variable by only being the value of another?

So in python if you create your list , then a new variable, reject_list and assign it the .pop() method, it changes the value in the previous list variable. 因此,在python中,如果您创建list ,则创建一个新变量reject_list并将其分配给.pop()方法,它将更改前一个list变量中的值。

list = ['item', 'thing', 'piece']
reject_list = list.pop()
print(list)

['item', 'thing']

I understand this effect, but not how it's possible. 我了解这种效果,但不太可能。 How does assigning the method of one variable as the value of another retroactively change the value of the original variable without first being defined, like this: 将一个变量的方法分配为另一个变量的值是如何追溯地更改原始变量的值而无需先定义的,如下所示:

list = ['item', 'thing', 'piece']
list = list.pop()    
print(list)

['item', 'thing']

For instance, this doesn't work with the .title() method: 例如,这不适用于.title()方法:

name = mike 
new_name = name.title() 
print(name)

mike
#the original name did not capitalize

so how is the .pop() method able to redefine a variable by only being the value of another? 那么.pop()方法如何仅通过作为另一个值来重新定义变量?

Your explanation of what you're seeing is not correct. 您对所见内容的解释不正确。 The change in the value of list has nothing to do with you assigning the result of list.pop() to reject_list . list的值更改与将list.pop()的结果分配给reject_list

Python is an object-based language which means the basic element of data in it are objects , which are a collection of data (or state ) and functions (called methods ). Python是一种基于对象的语言,这意味着其中的数据的基本元素是对象 ,它们是数据(或状态 )和函数(称为方法 )的集合。 Methods can access the state contained in the object they are called on and they can modify it. 方法可以访问被调用的对象中包含的状态,并且可以对其进行修改。 In the case of list, its elements are its state. 对于列表,其元素是其状态。 The behaviour of pop() is to modify this state by removing the last element of the list it was called on and returning it. pop()的行为是通过删除列表的最后一个元素并返回它来修改此状态。 Therefore, after calling it, the original list is shorter by one element. 因此,在调用它之后,原始列表将缩短一个元素。

Have a look at this example: 看一下这个例子:

>>> a = [1, 2, 3]
>>> b = a          # the name "b" refers to the same list as the name "a"
>>> a.pop()        # we pop off a value but don't assign it anywhere
3
>>> a
[1, 2]
>>> b
[1, 2]

Note that the result of a.pop() isn't assigned to anything but a is still changed. 请注意, a.pop()的结果未分配给任何东西,但a仍被更改。 You can think of the meaning of a.pop() as "remove the last element of the list a (while also returning it as a result)". 您可以将a.pop()的含义a.pop()为“删除列表a的最后一个元素(同时还返回它的结果)”。

Also note that it is not a good idea to use the name list since it is a builtin identifier referring to the list type itself and you are redefining it. 还要注意,使用名称list不是一个好主意,因为它是引用列表类型本身的内置标识符,您需要重新定义它。

The answer is that the list.pop() method specifically modifies the object; 答案是list.pop()方法专门修改了对象。 removes and returns an item from the list. 从列表中删除并返回一个项目。 str.title() returns a version of the string in title case, but does not modify the string itself. str.title()在标题大小写的情况下返回字符串的版本 ,但不会修改字符串本身。

It's also noteworthy that strings are immutable in Python. 同样值得注意的是,字符串在Python中是不可变的。

my_list = ["a", "b", "c"]
popped_element = my_list.pop()
print(my_list)
#['a', 'b']
print(popped_element)
#'c'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM