Issue with making object callable in python

后端 未结 3 1572
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 06:31

I wrote code like this

>>> class a(object):
        def __init__(self):
            self.__call__ = lambda x:x

>>> b = a()

I

3条回答
  •  无人及你
    2021-02-05 07:02

    What about this? Define a base class AllowDynamicCall:

    class AllowDynamicCall(object):
         def __call__(self, *args, **kwargs):
             return self._callfunc(self, *args, **kwargs)
    

    And then subclass AllowDynamicCall:

    class Example(AllowDynamicCall):
         def __init__(self):
             self._callfunc = lambda s: s
    

提交回复
热议问题