I\'m using Python 3. I know about the @classmethod decorator. Also, I know that classmethods can be called from instances.
class HappyClass(object):
@classme
You can add a function to a class at any point, a practice known as monkey-patching:
class SadClass:
pass
@classmethod
def say_dynamic(cls):
print('hello')
SadClass.say_dynamic = say_dynamic
>>> SadClass.say_dynamic()
hello
>>> SadClass().say_dynamic()
hello
Note that you are using the classmethod
decorator, but your function accepts no arguments, which indicates that it's designed to be a static method. Did you mean to use staticmethod
instead?