Markus Johansson
Markus Johansson

Reputation: 3773

Why do I get TypeError when calling function dynamically?

I have code similar to this. In MyModule.py

class SomeClass:
    @classmethod
    def SomeMethod(cls, a, b, c):
        return "foo"

Then I have another python file:

cls = getattr(MyModule, "SomeClass")
method = getattr(cls, "SomeMethod")
args = { "a":1, "b": 2, "c": 3 }
res = method(**args)
print "Result: " + res
print "Result type: " + str(type(res))

But I get the following error on the row calling type():

TypeError: 'unicode' object is not callable

To complicate things, I don't get the error with this minified example. Any ideas on how I can debug this? How can type() generate such an error?

Upvotes: 0

Views: 106

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122262

You have a local variable str or type (or both) that is bound to a unicode value. Remove it, that variable is masking the built-in callable.

You can use del str or del type to remove that reference, and the built-in can be used again.

Alternatively, use:

import __builtin__

print "Result type: " + __builtin__.str(__builtin__.type(res))

to be absolutely certain you are using the built-ins instead.

Upvotes: 1

Related Questions