[英]Weird list behavior in class
I've written a class that has a list as a variable. 我编写了一个具有列表作为变量的类。 I have a function that adds to that list and a function that outputs that list.
我有一个添加到该列表的函数和一个输出该列表的函数。
class MyClass:
myList = []
def addToList(self, myNumber):
self.myList.append(myNumber)
def outputList(self):
for someNumber in self.myList:
print someNumber
Now for some weird reason, if I declare two separate objects of the class: 现在出于某种奇怪的原因,如果我声明该类的两个单独的对象:
ver1 = MyClass()
ver2 = MyClass()
and then call addToList on ver1: 然后在ver1上调用addToList:
ver1.addToList(3)
and then output ver2's list: 然后输出ver2的列表:
ver2.outputList()
I get 3 as output for version 2's list! 我得到3作为版本2列表的输出! What is happening?
怎么了?
Your syntax is wrong. 您的语法错误。 You are using a class-static variable.
您正在使用类静态变量。 Consider this:
考虑一下:
class MyClass:
myList = [] # Static variable
def __init__(self):
self.myRealList = [] # Member variable
myList is actually a part of the MyClass definition, and thus is visible not only by the class name, but all instances of that class as well: myList实际上是MyClass定义的一部分,因此不仅可见于类名,而且该类的所有实例也可见:
c = MyClass()
c.myList = [1]
print MyClass.myList # will print [1]
You need to declare regular "member variables" in the __init__
constructor. 您需要在
__init__
构造函数中声明常规的“成员变量”。
Don't feel bad, I came to python from a C/C++/C# world, made this same mistake, and it confused me too at first. 不错,我是从C / C ++ / C#领域来到python的,犯了同样的错误,一开始它也使我感到困惑。
The problem is that mylist is initialized when the file is parsed. 问题是解析文件时会初始化mylist。 So each subsequent initializations of MyClass will have the same reference to the same mylist.
因此,MyClass的每个后续初始化将具有对相同mylist的相同引用。
class MyClass:
myList = []
Instead you should initialize it with the class. 相反,您应该使用该类对其进行初始化。
class MyClass:
def __init__(self):
self.myList = []
myList is actually a class variable (I don't know if that's the Pythonic term for it) because it is declared outside of any function. myList实际上是一个类变量(我不知道这是否是Pythonic术语),因为它是在任何函数外部声明的。
To make a variable instance-local (once again, I don't know the proper term), you should initialize it in your init method like "self.myList = []". 若要使局部变量成为实例(再一次,我不知道合适的术语),则应在init方法中将其初始化,例如“ self.myList = []”。
Please edit any bad formatting on my part, I am on my phone. 我正在用手机编辑任何格式错误的文件。
There are several things going on here. 这里发生了几件事。 You can have instance or class variables in python.
您可以在python中拥有实例或类变量。 In your example
myList
is a class variable which is static across all instances. 在您的示例中,
myList
是一个类变量,在所有实例中都是静态的。 There are also instance variables that are local to a class and you can override class variables with instance variables. 还有一些实例变量在类中是局部的,您可以用实例变量覆盖类变量。 Here is an example:
这是一个例子:
class Foo:
bar = 'Hello' # Class variable
def __init__(self):
self.foobar = 'Hello World' # instance variable
def createInstanceBar(self, x):
self.bar = x # Override class variable and create instance variable
a = Foo()
b = Foo()
print a.bar # Prints Hello
print b.bar # Prints Hello
print a.foobar # Prints Hello World
print b.foobar # Prints Hello World
a.createInstanceBar('Hello New World')
print a.bar # Prints Hello New World
print b.bar # Prints Hello
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.