UnboundLocalError: local variable 'k' referenced before assignment
<p>I have read <a href="https://stackoverflow.com/questions/17097273/unboundlocalerror-local-variable-referenced-before-assignment">StackQ1</a> and <a href="https://stackoverflow.com/questions/20873285/unboundlocalerror-local-variable-input-referenced-before-assignment">stackQ2</a> But unable to solve my error. 
The given below program is giving </p>

<blockquote>
  <p>UnboundLocalError: local variable 'k' referenced before assignment</p>
</blockquote>

<p>I already made variable <code>k</code> as <code>global</code> but it is not working.</p>

<pre><code>class myClass:
    global k
    k=0
    def data(self):
        def data2(k):
                for j in range(5):
                    k=k+1
                    return k
        for i in range(5):
            k=k+1
            data2(k)

Obj = myClass()
print(Obj.data())
</code></pre>

<p>I also tried as </p>

<pre><code>k=0
class myClass:
#     global k

    def data(self):
        def data2(k):
                for j in range(5):
                    k=k+1
                    return k
        for i in range(5):
            k=k+1
            data2(k)

Obj = myClass()
print(Obj.data())
</code></pre>

<p>But not working.</p>

<p>For my explanation, I reindent and annotate your code:</p>

<pre><code>class myClass:  # &lt;- [1]
    global k  # &lt;- [2]
    k = 0  # &lt;- [3]

    def data(self):
        def data2(k):  # &lt;- [4]
            for j in range(5):
                k = k + 1
                return k  # &lt;- [5]

        for i in range(5):
            k = k + 1  # &lt;- [6]
            data2(k)


Obj = myClass()
print(Obj.data())
</code></pre>

<h2>Code Review</h2>

<p>[1] Class names should use CamelCase according to <a href="https://www.python.org/dev/peps/pep-0008/#naming-conventions" rel="nofollow">PEP8</a> recommandation. Note that your class should inherit from <code>object</code> (in Python 2.7) to use (modern) new-style class.</p>

<p>[2] Global variable <code>k</code> is not defined at module level. You can't declare a variable in Python, you must define it. Since <code>k</code> looks like a integer, you can write:</p>

<pre><code>k = 0
</code></pre>

<p>[3] Here, you define a class variable.</p>

<p>Python use <a href="http://www.rafekettler.com/magicmethods.html#construction" rel="nofollow">magic methods</a>
   to construct and initialize an class instance.
   You should use <code>__init__</code> method to initialise your class, as below:</p>

<pre><code>class MyClass(object):
    def __init__(self):
        self.k = 0
</code></pre>

<p>[4] Here, you define a function inside the <code>data</code> method. What do you want to do? A new method?</p>

<pre><code>class MyClass(object):
    def data(self):
        def data2(k):  # &lt;- [4]
            pass
</code></pre>

<p>[5] I think you return prematurely: the loop ends at first iteration. Mind your indentation:</p>

<pre><code>def data2(k):
    for j in range(5):
        k = k + 1
    return k  # &lt;- [5]
</code></pre>

<p>[6] The local variable <code>k</code> is not defined in the <code>data</code> function.
If you want to use the module level variable (and modify it), you must use <code>global</code> keyword.</p>

<pre><code>class MyClass(object):
    def data(self):
        global k
        for i in range(5):
            k = k + 1  # &lt;- [6]
            data2(k)
</code></pre>

<p>Letâs start with a <a href="https://docs.python.org/2/tutorial/classes.html" rel="nofollow">tutorial</a>.</p>

python