user1876508 user1876508 - 1 month ago 4x
Python Question

How can I dynamically create class methods for a class in python

If I define a little python program as

class a():
def _func(self):
return "asdf"

# Not sure what to resplace __init__ with so that a.func will return asdf
def __init__(self, *args, **kwargs):
setattr(self, 'func', classmethod(self._func))

if __name__ == "__main__":

I receive the traceback error

Traceback (most recent call last):
File "", line 9, in <module>
AttributeError: class a has no attribute 'func'

What I am trying to figure out is, how can I dynamically set a class method to a class without instantiating an object?


The answer for this problem is

class a():

def func(cls, some_other_argument):
return some_other_argument

setattr(a, 'func', classmethod(func))

if __name__ == "__main__":

returns the following output

<bound method type.func of <class '__main__.a'>>


You can dynamically add a classmethod to a class by simple assignment to the class object or by setattr on the class object. Here I'm using the python convention that classes start with capital letters to reduce confusion:

# define a class object (your class may be more complicated than this...)
class A(object):

# a class method takes the class object as its first variable
def func(cls):
    print 'I am a class method'

# you can just add it to the class if you already know the name you want to use
A.func = classmethod(func)

# or you can auto-generate the name and set it this way
the_name = 'other_func' 
setattr(A, the_name, classmethod(func))