Python函数不访问类变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16680221/
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
Python function not accessing class variable
提问by Son of a Sailor
I am trying to access a class variable in an outside function, however I get the AttributeError, "Class has no attribute" My codes looks something like this:
我试图访问外部函数中的类变量,但是我得到了 AttributeError,“类没有属性”我的代码看起来像这样:
class example():
def __init__():
self.somevariable = raw_input("Input something: ")
def notaclass():
print example.somevariable
AttributeError: class example has no attribute 'somevariable'
Other questions have been asked similar to this however all the answers said to use self and define during init, which I did. Why can't I access this variable.
已经提出了与此类似的其他问题,但是所有答案都说在init期间使用 self 和 define ,我就是这样做的。为什么我不能访问这个变量。
采纳答案by Carlos V
If you want to create a class variable, you must to declare it outside any class methods (but still inside the class definition):
如果要创建类变量,则必须在任何类方法之外(但仍在类定义内)声明它:
class Example(object):
somevariable = 'class variable'
With this you can now access your class variable.
有了这个,您现在可以访问您的类变量。
>> Example.somevariable
'class variable'
The reason why your example isn't working is because you are assigning a value to an instancevariable.
您的示例不起作用的原因是您正在为instance变量赋值。
The difference between the two is that a classvariable is created as soon as the class object is created. Whereas an instancevariable will be created once the object has been instantiatedand only after they have been assigned to.
两者的区别在于class,一旦创建了类对象,就会创建一个变量。而instance一旦对象被实例化并且只有在它们被分配之后才会创建变量。
class Example(object):
def doSomething(self):
self.othervariable = 'instance variable'
>> foo = Example()
Here we created an instance of Example, however if we try to access othervariablewe will get an error:
这里我们创建了一个 实例Example,但是如果我们尝试访问,othervariable我们会得到一个错误:
>> foo.othervariable
AttributeError: 'Example' object has no attribute 'othervariable'
Since othervariableis assigned inside doSomething- and we haven't called ityet -, it does not exist.
由于othervariable是在内部分配的doSomething- 我们还没有调用 ityet -,所以它不存在。
>> foo.doSomething()
>> foo.othervariable
'instance variable'
__init__is a special method that automatically gets invoked whenever class instantiation happens.
__init__是一个特殊的方法,每当类实例化发生时它会自动被调用。
class Example(object):
def __init__(self):
self.othervariable = 'instance variable'
>> foo = Example()
>> foo.othervariable
'instance variable'
回答by g.d.d.c
You're a bit confused on what is a class attribute, and what is not.
您对什么是类属性以及什么不是类属性感到有些困惑。
class aclass(object):
# This is a class attribute.
somevar1 = 'a value'
def __init__(self):
# this is an instance variable.
self.somevar2 = 'another value'
@classmethod
def usefulfunc(cls, *args):
# This is a class method.
print(cls.somevar1) # would print 'a value'
def instancefunc(self, *args):
# this is an instance method.
print(self.somevar2) # would print 'another value'
aclass.usefulfunc()
inst = aclass()
inst.instancefunc()
The class variables are always accessible from the class:
类变量始终可以从类访问:
print(aclass.somevar1) # prints 'a value'
Likewise, all instances have access to all instance variables:
同样,所有实例都可以访问所有实例变量:
print(inst.somevar2) # prints 'another value'

