Python How to re-assign a static variable value of a python class with a return value of method
<p>I am trying to declare a static variable inside the class. I am trying to add a return value of a code inside a function. </p>

<p>The return value is assigned to the static variable inside the class but when i try to create an object of the class and access the static variable value from another class, it still returns the old value.</p>

<pre><code>class Login(Base):
      token = ''
      def run(self):
           token=keystone.get_raw_token_from_identity_service('http://localhost:35357/v3,
username=username,                                             user_domain_name='default,                                         password=passwrd,                                                                      project_name=project,                                                     project_domain_name='default')

      def auth_token(self,token):
          self.token = token
          return self.token
</code></pre>

<p>In another class, i am trying to call the static variable but it always prints the initial value declared in the first class i.e. token = ''</p>

<pre><code>      credentials = Login(Base)
      print(type(credentials.auth_token.__func__))
      print(hasattr(credentials,'token'))
      print(credentials.token)""" 
</code></pre>

<p>If you make auth_token a @classmethod, it will behave the way you want</p>

<pre><code>  @classmethod 
  def auth_token(cls, token):
      cls.token = token
      return cls.token
</code></pre>

<p>I'm not sure I love the design, but that's your affair.</p>

<p>EDIT: Here's an example of this code in action, in response to comments:</p>

<pre><code>&gt;&gt;&gt; class Login(object):
...   token = ""
...   @classmethod 
...   def auth_token(cls, token):
...       cls.token = token
...       return cls.token
... 
&gt;&gt;&gt; l = Login()
&gt;&gt;&gt; l2 = Login()
&gt;&gt;&gt; l.token
''
&gt;&gt;&gt; l2.token
''
&gt;&gt;&gt; l.auth_token("foo")
'foo'
&gt;&gt;&gt; l2.token
'foo'
&gt;&gt;&gt; 
</code></pre>

python python-2.7