user1126425 user1126425 -4 years ago 93
Python Question

'classmethod' object is not callable

Ok so I have this code:

class SomeClass:
def func1(cls,arg1):
#---Do Something---
def func2(cls,arg1):
#---Do Something---

# A 'function map' that has function name as its keys and the above function
# objects as values

def func3(cls,arg1):
# following is a dict(created by reading a config file) that contains func names
# as keys and boolean as values that tells the program whether or not to run
# that function
global funcList
for func in funcList:
if funcList[func]==True:
cls.func_map[func](arg1) #TROUBLING PART!!!

if _name__='main'

When I run this I keep getting the error:

Exception TypeError: "'classmethod' object is not callable"

I am unable to figure out whats wrong with this and would appreciate your help.

Answer Source

You can't create references to classmethods until the class has been defined. You'll have to move it out of the class definition. However using a global function map to decide what gets run is really awkward. If you described what you are trying to do with this, we could probably suggest a better solution.

class SomeClass(object):
    def func1(cls, arg1):
        print("Called func1({})".format(arg1))

    def func2(cls, arg1):
        print("Call func2({})".format(arg1))

    def func3(cls, arg1):
        for fnName,do in funcList.iteritems():
            if do:
                except KeyError:
                    print("Don't know function '{}'".format(fnName))

# can't create function map until class has been created
SomeClass.func_map = {
    'func1': SomeClass.func1,
    'func2': SomeClass.func2

if __name__=='__main__':
    funcList = {'func1':True, 'func2':False}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download