trgtulas
trgtulas

Reputation: 37

'list' object that is in the class is not callable

I wrote a class that includes some functions in it. I use the 'result' variable in many places, so I make it a public variable in the class. But when I call it, like 'list(self.result.keys())', I'm getting the 'list object is not callable' error. If I call the 'result' as 'self.result.keys()', don't get any error. But when I call it as a list object, it becomes a problem. Here is my code.
I'll be waiting for your answers, have a nice day.

list1 = [2, 4, 6, 8]
list2 = [(2, 2), (2, 4), (2, 6), (2, 8), (4, 4), (4, 8), (6, 6), (8, 8)]    
class MyClass():
    def __init__(self):
        self.list1 = list1
        self.list2 = list2
        self.reflexive_list = []
        j = 0
        z = 0
        while j < len(self.list1):
            i = 0
            while i < len(self.list1):
                if self.list1[i] == self.list1[j]:
                    tup = self.list1[j],self.list1[i]
                    self.reflexive_list.append(tup)
                i += 1
            else:
                None
            j = j + 1
        else:
            None
            
        self.result = {}
        for first, second in self.list2:
            self.result.setdefault(first, []).append(second)
        print(list(self.result.keys()))

Upvotes: 0

Views: 150

Answers (1)

AKX
AKX

Reputation: 168834

You're shadowing the global list() callable in

def __init__(self, list):

with whatever you pass in as list.

The start of your __init__ function really only works by virtue of list1 and list2 being globals.

Upvotes: 3

Related Questions