Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python function not accessing class variable

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:

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.

like image 418
Son of a Sailor Avatar asked May 21 '13 22:05

Son of a Sailor


People also ask

How do you access class variables outside class in Python?

If you want to use that variable even outside the class, you must declared that variable as a global. Then the variable can be accessed using its name inside and outside the class and not using the instance of the class. class Geek: # Variable defined inside the class.

How do you access class variables?

To access class variables, you use the same dot notation as with instance variables. To retrieve or change the value of the class variable, you can use either the instance or the name of the class on the left side of the dot.

How do you access variables in Python?

In Python, a variable declared outside of the function or in global scope is known as a global variable. This means that a global variable can be accessed inside or outside of the function.


2 Answers

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 instance variable.

The difference between the two is that a class variable is created as soon as the class object is created. Whereas an instance variable will be created once the object has been instantiated and only after they have been assigned to.

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 othervariable we will get an error:

>> foo.othervariable
AttributeError: 'Example' object has no attribute 'othervariable'

Since othervariable is assigned inside doSomething - and we haven't called ityet -, it does not exist.

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

__init__ is a special method that automatically gets invoked whenever class instantiation happens.

class Example(object):

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

>> foo = Example()
>> foo.othervariable
'instance variable'
like image 63
Carlos Avatar answered Oct 26 '22 23:10

Carlos


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'
like image 42
g.d.d.c Avatar answered Oct 26 '22 23:10

g.d.d.c