On 9/18/22, Stefan Ram <r...@zedat.fu-berlin.de> wrote: > r...@zedat.fu-berlin.de (Stefan Ram) writes (abbreviated): >>types.MethodType( function, instance ) >>functools.partial( function, instance ) >>new_method.__get__( instance ) > > I wonder which of these three possibilities expresses > the idea of creating a new method from a function and > an instance most clearly.
Using types.MethodType(function, instance) is the most clear and correct of the three. Using the function's descriptor __get__() method is equivalent in terms of the result. That said, the descriptor protocol is an intermediate-level concept, so manually calling __get__() isn't friendly to novices or casual users of the language. Using a functools.partial isn't the expected method type, with __func__ and __self__ attributes, and, unlike a method, it doesn't expose the wrapped function's __code__, __name__, __module__, __doc__, __annotations__, __defaults__, __kwdefaults__, __closure__, __globals__, or __builtins__. If dynamic inspection matters, using a functools.partial won't work directly with dis.dis(), inspect.getfile(), inspect.getsource(), inspect.getdoc(), inspect.get_annotations(), inspect.getcallargs(), or inspect.getclosurevars(). -- https://mail.python.org/mailman/listinfo/python-list