Python 在一种方法中创建的属性在另一种方法中不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16158684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attribute created in one method doesn't exist in other method
提问by user2309239
Here I have an attribute 'a', which is defined in first class method and should be changed in second. When calling them in order, this message appears:
这里我有一个属性“a”,它是在第一类方法中定义的,应该在第二类方法中更改。按顺序调用它们时,会出现以下消息:
AttributeError: 'Class' object has no attribute 'a'
AttributeError: 'Class' 对象没有属性 'a'
The only way I've found - define 'a' again in second method, but in real code it has long inheritance and app will be messed. Why doesn't it work? Isn't self.aequal to Class.a?
我找到的唯一方法 - 在第二种方法中再次定义 'a',但在实际代码中,它具有很长的继承性,应用程序会被弄乱。为什么不起作用?self.a不等于Class.a吗?
class Class(object):
def method_1(self):
self.a = 1
def method_2(self):
self.a += 1
Class().method_1()
Class().method_2()
采纳答案by Felipe
Short answer, no. The problem with your code is that each time you create a new instance.
简短的回答,没有。您的代码的问题在于每次创建一个新实例时。
Edit: As abarnert mentions below, there is a big difference between Class.aand c.a. Instance attributes (the second case) belong to each specific object, whereas class attributes belong to the class. Look at abarnert's comment below or the discussion herefor more info.
编辑:正如 abarnert 在下面提到的,Class.a和之间有很大的区别c.a。实例属性(第二种情况)属于每个特定对象,而类属性属于类。查看下面 abarnert 的评论或此处的讨论以获取更多信息。
Your code is equivalent to
你的代码相当于
c1 = Class()
c1.method_1() # defines c1.a (an instance attribute)
c2 = Class()
c2.method_2() # c2.a undefined (the c2 instance doesn't have the attribute)
You probably want to do somthing like
你可能想做类似的事情
c = Class()
c.method_1() # c.a = 1
c.method_2() # c.a = 2
print "c.a is %d" % c.a # prints "c.a is 2"
Or probably even better would be to initialize cwith an aattribute
或者可能更好的是c用a属性初始化
class Class:
def __init__(self):
self.a = 1 # all instances will have their own a attribute
回答by Sean Vieira
A newly-createdinstance of Classhas no attribute awhen you do instance_of_class.method_2()without calling method_1, as in your example.
一个新创建的实例Class有没有属性a,当你做instance_of_class.method_2()不通话method_1,因为在你的榜样。
Consider this slightly altered version of your code:
考虑一下你的代码的这个稍微改变的版本:
class CreateNewClassInstance(object):
def create_a(self):
self.a = 1
def add_one_to_a(self):
self.a += 1
CreateNewClassInstance().create_a()
CreateNewClassInstance().add_one_to_a()
Each time you call Class()(or CreateNewClassInstance()) you create a newobject, with its own attribute a. Until you initialize a, you don't have an attribute with that name.
每次调用Class()(或CreateNewClassInstance()) 时,都会创建一个具有自己属性的新对象a。在您 initialize 之前a,您没有具有该名称的属性。
Most of the time this isn't an issue - however, +=will attempt to load self.abefore adding one to it - which is what is causing your AttributeErrorin this case.
大多数情况下,这不是问题 - 但是,+=会self.a在添加之前尝试加载- 这就是AttributeError在这种情况下导致您的原因。

