Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python函数不访问类变量_Python_Python 2.7 - Fatal编程技术网

Python函数不访问类变量

Python函数不访问类变量,python,python-2.7,Python,Python 2.7,我试图在外部函数中访问一个类变量,但是我得到了AttributeError,“class has no attribute”我的代码如下所示: class example(): def __init__(): self.somevariable = raw_input("Input something: ") def notaclass(): print example.somevariable AttributeError: class example

我试图在外部函数中访问一个类变量,但是我得到了AttributeError,“class has no attribute”我的代码如下所示:

class example():
     def __init__():
          self.somevariable = raw_input("Input something: ")

def notaclass():
    print example.somevariable

AttributeError: class example has no attribute 'somevariable'

其他问题也被问到了类似的问题,但是所有的答案都说在init期间使用self和define,我确实这么做了。为什么我不能访问此变量

您对什么是类属性和什么不是有些困惑

  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()
始终可以从类访问类变量:

print(aclass.somevar1) # prints 'a value'
同样,所有实例都可以访问所有实例变量:

print(inst.somevar2) # prints 'another value'

如果要创建类变量,必须在任何类方法外部(但仍在类定义内部)声明它:

有了它,您现在可以访问类变量了

>> Example.somevariable
'class variable'

示例不起作用的原因是,您正在为
实例
变量赋值

两者之间的区别在于,一创建类对象,就会创建一个
class
变量。而
实例
变量将在对象实例化后创建,并且仅在将其分配给之后创建

class Example(object):
      def doSomething(self):
          self.othervariable = 'instance variable'

>> foo = Example()
这里我们创建了一个
Example
的实例,但是如果我们尝试访问
othervariable
,我们将得到一个错误:

>> foo.othervariable
AttributeError: 'Example' object has no attribute 'othervariable'
由于
othervariable
是在
doSomething
内部赋值的,我们还没有调用它,所以它不存在

>> foo.doSomething()
>> foo.othervariable
'instance variable'

\uuuu init\uuuu
是一种特殊的方法,每当发生类实例化时,它都会自动被调用

class Example(object):

      def __init__(self):
          self.othervariable = 'instance variable'

>> foo = Example()
>> foo.othervariable
'instance variable'

self.somevariable
是一个实例变量,而不是类变量。要理解这一点,您必须认识到
example
类是一个对象,但它属于
类型
,而不是
example
类型。使用创建的
example
类的每个实例,例如
inst=example()
都是
example
类型的对象。您的代码在类型为
example
的每个对象中创建一个
somevariable
属性。您可能还希望显示
aclass.somevar1
inst.somevar2
独立工作。答案很好。谢谢你,卡洛斯!
class Example(object):

      def __init__(self):
          self.othervariable = 'instance variable'

>> foo = Example()
>> foo.othervariable
'instance variable'