[英]Same name for objects in python
In 100 days of code with python (anjella yu), teacher appends objects with same name to a list and i can't figure out how same name objects can be created.在 python (anjella yu) 的 100 天代码中,老师将具有相同名称的对象附加到列表中,我无法弄清楚如何创建相同名称的对象。 Can somebody explain me?
有人可以解释一下吗?
my_list = []
for _ in range(10):
new_object = turtle()
my_list.append(new_object)
As you can see 10 objects with same name are created and appended to my_list如您所见,创建了 10 个具有相同名称的对象并将其附加到 my_list
new_object
is just one name that refers to an instance of turtle
. new_object
只是一个引用turtle
实例的名称。 my_list.append(new_object)
adds another reference to the same object to the list; my_list.append(new_object)
将另一个对同一个 object 的引用添加到列表中; that reference does not change when the name new_object
is made to refer to a different object.当名称
new_object
引用不同的 object 时,该引用不会改变。
Put another way, you are not adding the name new_object
to the list, but a reference to the object the name refers to.换句话说,您并没有将名称
new_object
添加到列表中,而是添加了对该名称所指的 object 的引用。 (You could also have written my_list.append(turtle())
without defining the variable new_object
at all; the result would be the same.) (您也可以编写
my_list.append(turtle())
而根本不定义变量new_object
;结果是一样的。)
Once the loop completes, you have a list containing references to 10 different objects, and the name new_object
still refers to the last object created.循环完成后,您将获得一个包含对 10 个不同对象的引用的列表,名称
new_object
仍然引用最后创建的 object。
A list is just a collection of objects.列表只是对象的集合。 The only way you can refer to each is by its numeric index (eg my_list[0], my_list[1], etc.).
您可以引用每个的唯一方法是通过其数字索引(例如 my_list[0]、my_list[1] 等)。 Therefore, they have no "names".
因此,他们没有“名字”。 So, assuming that turtle() returns an object, the new object just gets added to the next position in my_list.
因此,假设 turtle() 返回 object,新的 object 将添加到 my_list 中的下一个 position。
This is different than sets and dictionaries.这不同于集合和字典。 In a set, an object can exist only once.
在一个集合中,一个 object 只能存在一次。 So, if you send the same object twice, it will only end up in the set once.
所以,如果你发送相同的 object 两次,它只会在集合中结束一次。
Similarly for dictionaries.字典也是如此。 The "key" in a dictionary is a sort of name for the object it represents.
字典中的“键”是它所代表的 object 的一种名称。 If you provide two objects with the same key, the second one you provide will overwrite the first.
如果您提供两个具有相同键的对象,您提供的第二个将覆盖第一个。
I have slightly modified your code here, but the essense is the same.我在这里稍微修改了你的代码,但本质是一样的。
>>> my_list = []
>>> for _ in range(10):
... new_object = dict()
... my_list.append(new_object)
...
>>> my_list
[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
>>> my_list[0]
{}
>>> my_list[1]
{}
>>> id(my_list[0])
140200988731072
>>> id(my_list[1])
140200988731136
>>> id(my_list[3])
140200988731264
>>> id(my_list[4])
140200988731328
>>> id(my_list[2])
140200988731200
>>>
As you can see that my_list's first, second or third object are visible the same and none of them have any content but they are different.如您所见,my_list 的第一个、第二个或第三个 object 是可见的,它们都没有任何内容,但它们是不同的。
In Python id
(inbuilt function) is used to identify the unique hash code given to each object living in the python memory. So as I check them, each object has a unique id.在 Python 中,
id
(内置函数)用于识别生活在 python memory 中的每个 object 的唯一 hash 代码。所以当我检查它们时,每个 object 都有一个唯一的 id。
As you may now understand, every object is Python is identified by its unique id not by its visible name.正如您现在可能理解的那样,每个 object 都是 Python 由其唯一 ID 标识,而不是由其可见名称标识。 Also Python list uses pointers(internally) to hold on to its objects.
Python 列表也使用指针(内部)来保存其对象。 So each unique/new object has a new memory location or new pointer location.
所以每个独特的/新的 object 都有一个新的 memory 位置或新的指针位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.