How to add a classmethod in Python dynamically

前端 未结 4 1305
無奈伤痛
無奈伤痛 2021-02-14 04:15

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         


        
4条回答
  •  独厮守ぢ
    2021-02-14 04:49

    As a side note, you can just use an instance attribute to hold a function:

    >>> class Test:
    ...    pass
    ... 
    >>> t=Test()
    >>> t.monkey_patch=lambda s: print(s)
    >>> t.monkey_patch('Hello from the monkey patch')
    Hello from the monkey patch
    

提交回复
热议问题