NameError: name 'self' is not defined" when using inside with statement
<p>Can someone help me about using 'self' inside 'with'. 
Code below throws "NameError: name 'self' is not defined".</p>

<pre><code>class Versions:

    def __init__(self):
        self.module_a = '1.0.0'
        self.module_b = '1.0.0'

        if os.path.exists('config.json'):
            with open('config.json') as f:
                configs = json.load(f)
                for config in configs:
                    setattr(self, config, configs[config])
</code></pre>

<p>Traceback</p>

<pre><code>NameError                                 Traceback (most recent call last)
&lt;ipython-input-5-ed6a4ca551d4&gt; in &lt;module&gt;()
      3                 configs = json.load(f)
      4                 for config in configs:
----&gt; 5                         setattr(self, config, configs[config])
      6

NameError: name 'self' is not defined
</code></pre>

<p>Thanks.</p>

<p>please check Your indentation - if You mix spaces and tabs, this can happen; use $python -tt to verify. Your code snippet works fine for me.</p>

python class nameerror contextmanager