Python奇怪的行为与列表和追加(Python Strange Behavior with List & Append)

编程入门 行业动态 更新时间:2024-10-26 12:24:33
Python奇怪的行为与列表和追加(Python Strange Behavior with List & Append)

以下代码是我遇到的问题,正在寻找解释。 代码的行为与我预期的不同。 下面的代码将是我的预期输出和实际输出。 最后要注意的是,我知道这个代码可能是'奇怪的',使用范围(1)有点奇怪,至少可以说。 原因是程序中的这种确切发生(范围是变量,但是在这些值)引起了问题..所以我让这个简单的代码来复制它。

userList = [] class User(): listA = [] listB = [] def setup(self): for i in range(1): self.listA.append('a') self.listB.append('b') for i in range(5): user = User() userList.append(user) for i in range(len(userList)): userList[i].setup() for i in range(len(userList)): print str(userList[i].listA) print str(userList[i].listB)

预期产出

['a'] ['b'] ['a'] ['b'] ['a'] ['b'] ['a'] ['b'] ['a'] ['b']

实际产出

['a','a','a','a','a'] ['b','b','b','b','b'] ['a','a','a','a','a'] ['b','b','b','b','b'] ['a','a','a','a','a'] ['b','b','b','b','b'] ['a','a','a','a','a'] ['b','b','b','b','b'] ['a','a','a','a','a'] ['b','b','b','b','b']

讨论

我很感激为什么会发生这种情况的任何解释。 我不确定内置的append()函数是以何种方式影响所有用户,或者如果每个用户以某种方式共享他们的字段。 在Python 2.7.3上运行。

The following code is an issue I encountered and am looking for an explanation. The behavior of the code different than what I expected. Below the code will be my expected output, and the actual output. One last thing to note, is that I understand this code may be 'strange', and that using range(1) is a bit odd to say the least. The reason for this is that this exact occurrence in a program (the ranges were variables but at these values) caused problems.. so I made this simple code to replicate it.

userList = [] class User(): listA = [] listB = [] def setup(self): for i in range(1): self.listA.append('a') self.listB.append('b') for i in range(5): user = User() userList.append(user) for i in range(len(userList)): userList[i].setup() for i in range(len(userList)): print str(userList[i].listA) print str(userList[i].listB)

Expected Output

['a'] ['b'] ['a'] ['b'] ['a'] ['b'] ['a'] ['b'] ['a'] ['b']

Actual Output

['a','a','a','a','a'] ['b','b','b','b','b'] ['a','a','a','a','a'] ['b','b','b','b','b'] ['a','a','a','a','a'] ['b','b','b','b','b'] ['a','a','a','a','a'] ['b','b','b','b','b'] ['a','a','a','a','a'] ['b','b','b','b','b']

Discussion

I appreciate any explanation as to why this is happening. I'm not sure if the built-in append() function is somehow affecting all Users, or if each User is somehow sharing their fields. Running on Python 2.7.3.

最满意答案

将此与您的代码进行比较

class User(): def setup(self): self.listA = [] # instance variable self.listB = [] # instance variable for i in range(1): self.listA.append('a') self.listB.append('b')

请注意,没有必要在课堂级别“声明”任何变量

Compare this to your code

class User(): def setup(self): self.listA = [] # instance variable self.listB = [] # instance variable for i in range(1): self.listA.append('a') self.listB.append('b')

Note that it's not necessary to "declare" any variables at the class level

更多推荐

本文发布于:2023-08-07 23:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466149.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   列表   Python   Strange   Append

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!