[英]Creating new object in python is not returning a new object
I am new to python. 我是python的新手。
Why am I not getting a new object when I call tempMyObject = myObject()
? 为什么在调用
tempMyObject = myObject()
时没有得到新对象?
class myObject(object):
x = []
def getMyObject():
tempMyObject = myObject()
print "debug: %s"%str(tempMyObject.x)
tempMyObject.x.append("a")
return tempMyObject
#run
a = getMyObject()
b = getMyObject()
My debug prints out: 我的调试打印出来:
debug: []
debug: ["a"]
I don't understand why both of these debug arrays are not null, can someone please enlighten me? 我不明白为什么这两个调试数组都不为null,有人可以赐教吗?
EDIT: I found the mistake i put in python code on my post. 编辑:我发现我在帖子中放入python代码的错误。 I am using the .append("a") in my function
我在函数中使用.append(“ a”)
You have created x
as a class variable rather than an instance variable. 您已将
x
创建为类变量而不是实例变量。 To associate the variable with a particular instance of a class, do something like this: 要将变量与类的特定实例相关联,请执行以下操作:
class myObject(object):
def __init__(self): # The "constructor"
self.x = [] # Assign x to this particular instance of myObject
>>> debug: []
>>> debug: []
For a little better explanation of what's going on, have a look at this little mockup that demonstrates the same thing, a little more explicitly (if also more verbosely). 为了更好地解释发生了什么,请看看这个小型模型,它演示了同样的事情,更明确一些(如果也更冗长)。
class A(object):
class_var = [] # make a list attached to the A *class*
def __init__(self):
self.instance_var = [] # make a list attached to any *instance* of A
print 'class var:', A.class_var # prints []
# print 'instance var:', A.instance_var # This would raise an AttributeError!
print
a = A() # Make an instance of the A class
print 'class var:', a.class_var # prints []
print 'instance var:', a.instance_var # prints []
print
# Now let's modify both variables
a.class_var.append(1)
a.instance_var.append(1)
print 'appended 1 to each list'
print 'class var:', a.class_var # prints [1]
print 'instance var:', a.instance_var # prints [1]
print
# So far so good. Let's make a new object...
b = A()
print 'made new object'
print 'class var:', b.class_var # prints [1], because this is the list bound to the class itself
print 'instance var:', b.instance_var # prints [], because this is the new list bound to the new object, b
print
b.class_var.append(1)
b.instance_var.append(1)
print 'class var:', b.class_var # prints [1, 1]
print 'instance var:', b.instance_var # prints [1]
There a few bits missing in your code, like the class initialiser first and foremost. 您的代码中缺少一些内容,例如最重要的是类初始化程序。 The correct code is as follows:
正确的代码如下:
class myObject(object):
def __init__(self):
self.x=[] #Set x as an attribute of this object.
def getMyObject():
tempMyObject = myObject()
print "debug: %s"%str(tempMyObject.x) #Just after object initialisation this is an empty list.
tempMyObject.x = ["a"]
print "debug2: %s"%str(tempMyObject.x) #Now we set a value to it.
return tempMyObject
#run
a = getMyObject()
b = getMyObject()
Now the debug will first print out an empty list and then, once it was set, "a". 现在,调试将首先打印出一个空列表,然后将其设置为“ a”。 Hope this helps.
希望这可以帮助。 I recommend looking at basic python classes tutorial .
我建议看一下基本的python类教程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.