3

I'm working on a Python Image Library project for fun. I cannot however, figure out why I keep getting a "list not callable" error for this code:

def __computeTopColors(self):
    temp1, temp2, temp3, temp4 = [], [], [], []

    max = (0,0,0)
    for v in self.PixelVals: 
        if (v[0] > max[0]) and (v[1] > max[1]) and (v[2] > max[2]):
            max = v

    min = (1000,1000,1000)
    for v in self.PixelVals:
        if (v[0] < min[0]) and (v[1] < min[1]) and (v[2] < min[2]):
            min = v

    for pval in self.PixelVals:
        if (min[0] <= pval[0] <= int(((max[0])/2))) and (min[1] <= pval[1] <= int(((max[1])/2))):
            temp1.append(pval) 

    return temp1

def temp(self): 
    print self.temp

Note: This is actually an excerpt from a class, but I don't think that's particularly relevant. self.PixelVals is a list tuples of the form (R,G,B). self.temp is a temporary method I'm using to test the __computeTopColors method; I set self.temp equal to self.__computeTopColors in __init__.

The Traceback :

Traceback (most recent call last):
  File "fiveColors.py", line 39, in <module>
    a.temp()

Line 39 is the last line in my code block.

5
  • 1
    Can you post the full traceback? Commented Dec 19, 2012 at 0:49
  • At least tell us which line raises the exception, or we have to go through your code line by line trying to guess which one could be wrong. Commented Dec 19, 2012 at 1:05
  • Also, the fact that it's part of a class may not be relevant, but then it's up to you to extract it into something that can run on its own, and give us a Short, Self Contained, Correct Example. Commented Dec 19, 2012 at 1:09
  • The code in that traceback doesn't appear to be any of the code you showed us. You say it "is the last line in my code block", but the last line is print self.temp, which clearly is not the same as a.temp(). Commented Dec 19, 2012 at 1:15
  • @xetur: While you're at it, why not give us an actual runnable example that produces exactly the error you want us to debug, instead of giving us random bits of unrelated code and making us try to guess what might be wrong with the rest of your code? Commented Dec 19, 2012 at 1:17

2 Answers 2

6

You've shadowed int with a list somewhere. Stop doing that. And stop shadowing min() and max() while you're at it.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I'm new to Python's OOP, can you tell elaborate on this please? What exactly should I do?
@xetur: This has nothing to do with OOP, or even specifically with Python. If you name a variable int, that replaces the built-in definition of int, which means you can no longer call the built-in function. Likewise for min and max.
This was a good guess, considering that int was the only function call in the function he gave us… but it looks like the error didn't come from that function, so shadowing int is not the problem. (Well, probably… It's so hard to tell…)
2

I pasted that code into a file, and add the following:

>>> class Foo(object):
...    pass
>>> a = Foo()
>>> a.PixelVals = [(1,2,3),(4,5,6)]
>>> __computeTopColors(a)
[(1, 2, 3)]

So there is nothing wrong with this code.

Scratch that—there are stylistic problems, just none of them break the code. For example:

  • You should never give your variables names like min and max that are the same as names of built-in functions.
  • Don't put in tons of extra parens that have no function except to make things harder to read—int(((max[1])/2)) is obviously exactly equivalent to int(b[1]/2), but it takes a few seconds and a bit of thought to see what you're doing this way, and even longer to verify that you did it correctly (which is always a problem, but especially so when you're asking other people to look at your code and guess what might be wrong with it).
  • You should name variables like PixelVals in lowercase instead of CamelCase, etc.

So obviously, whatever's wrong is in some other part of your code that you didn't show us.

You added the last line of the traceback, and said that it was from "the last line in my code block". The last line is this:

print self.temp

The error complains about this:

a.temp()

Clearly, the code you showed us is not the code you're running. But I can make a guess about the problem from this:

That a is probably a class variable or global variable that holds a list. You're trying to call it as a function. So Python, quite sensibly, tells you that list objects are not callable.

Or, here's another guess:

I set self.temp equal to self.__computeTopColors

Well, that's going to replace the method temp with a different variable. As written, it would actually replace the method temp with the bound method __computeTopColors, which couldn't could the problem you're seeing (although it's a very strange and bad thing to do). But maybe this isn't actually true, and you actually set self.temp equal to self.__computeTopColors()—the result of calling that bound method. Which is probably a list. So now, some other code that you haven't shown us tries to call the temp method by doing a.temp(), and because you've replaced the temp method with a list instead of something callable, that raises the same error.

In fact, even if you didn't replace the temp method, it's not exactly useful as defined:

def temp(self): 
    print self.temp

There is no way this could print anything other than your implementation's representation of the bound method temp (e.g., <bound method Foo.temp of <__main__.Foo object at 0x106d0f2d0>>), which can't possibly be useful to you in any way.

Whether it's the first problem, the second, or something entirely different, this all points to the same larger issue: Don't give arbitrary, meaningless names to variables—and, if you do, be very careful not to reuse the same names to mean different things in different places. If you're lucky, you'll confuse Python and get an error. If you're unlucky, you'll only confuse yourself and other readers of your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.