1

I have read StackQ1 and stackQ2 But unable to solve my error. The given below program is giving

UnboundLocalError: local variable 'k' referenced before assignment

I already made variable k as global but it is not working.

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())

I also tried as

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())

But not working.

Humty
  • 1,321
  • 3
  • 18
  • 35

3 Answers3

0
class My_Class(object):
     def __init__(self, **kwargs):
         super(My_Class, self).__init__()
         self.k = "hello"
     def data(self, *args):
         print "call data"
         return self.k


my_class = My_Class()
print my_class.data() 
Ari Gold
  • 1,528
  • 11
  • 18
0

The problem is resolved and posting it for the newbies

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

Obj = myClass()
print(Obj.data()) 

Credit goes to @Moses Koledoye and @ Abhishek

Humty
  • 1,321
  • 3
  • 18
  • 35
0

For my explanation, I reindent and annotate your code:

class myClass:  # <- [1]
    global k  # <- [2]
    k = 0  # <- [3]

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

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


Obj = myClass()
print(Obj.data())

Code Review

[1] Class names should use CamelCase according to PEP8 recommandation. Note that your class should inherit from object (in Python 2.7) to use (modern) new-style class.

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

k = 0

[3] Here, you define a class variable.

Python use magic methods to construct and initialize an class instance. You should use __init__ method to initialise your class, as below:

class MyClass(object):
    def __init__(self):
        self.k = 0

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

class MyClass(object):
    def data(self):
        def data2(k):  # <- [4]
            pass

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

def data2(k):
    for j in range(5):
        k = k + 1
    return k  # <- [5]

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

class MyClass(object):
    def data(self):
        global k
        for i in range(5):
            k = k + 1  # <- [6]
            data2(k)

Let’s start with a tutorial.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103