Easily editing base class variables from inherited class
<h3>How does communication between base classes and inherited classes work?</h3>

<p>I have a data class in my python code ( storing all important values, duh ), I tried inheriting new subclasses from the <em>data base class</em>, everything worked fine except the fact that the classes were not actually communicating ( when one class variable was changed in a subclass, the class attribute <strong>was not</strong> changed in the base class nor any other subclasses.</p>

<p>I guess I just failed to understand how inheritance works, <strong>my question is</strong>: Does inheritance keep any connection to the base classes, or are the values set at the time of inheritance? </p>

<p>If there is any connection, how do you easily manipulate base class variables from a subclass ( I tried it with the <em>cls</em> variable to access base class variables, didn't work out )</p>

<h3>Example</h3>

<pre><code>class Base:
    x = 'baseclass var' # The value I want to edit

class Subclass(Base):
    @classmethod(cls)
        ???edit_base_x_var_here??? # This is the part I don't know
</code></pre>

<p>Well, you could do that in this way:</p>

<pre><code>class Base:
    x = 'baseclass var' # The value I want to edit

class Subclass(Base):
    @classmethod
    def change_base_x(cls):
        Base.x = 'nothing'


print Subclass.x
Subclass.change_base_x()
print Subclass.x
</code></pre>

<p>furthermore, you don't have to use <code>@classmethod</code>, it could be staticmethod, because you don't need current class object <code>cls</code>:</p>

<pre><code>class Base:
    x = 'baseclass var' # The value I want to edit

class Subclass(Base):
    @staticmethod
    def change_base_x():
        Base.x = 'nothing'
</code></pre>

<p>EDITED:</p>

<p>According to your question, about other way. Yes it is, but not so pretty. I would say more. If you want to change variable of base class, then you will do it globally though, so that option with assigning to <code>Base.x</code> is the best way you can achieve that.</p>

python class object inheritance attributes