def add_handler(self, handler: EventHandler, parent: Subscriber) -> _HandlerFunc: """Adds the handler, with given parent, to own subscribers.""" f = partial(handler.func, parent) for etype in handler.etypes: self._handlers[etype].append(f) if parent not in self._handlers[etype]: self._subscribers[etype].append(parent) return f
def test_doc(): def foo(x, y): """ a `foo` function :param x: :param y: :return: """ return x + y from makefun import partial bar = partial(foo, x=12) bar.__name__ = 'bar' help(bar) assert bar(1) == 13
def funcopy(f): """ >>> def foo(): ... return 1 >>> foo.att = 2 >>> f = funcopy(foo) >>> f.att 2 >>> f() 1 """ # see https://stackoverflow.com/a/6527746/7262247 # and https://stackoverflow.com/a/13503277/7262247 # apparently it is not possible to create an actual copy with copy() ! # Use makefun.partial which preserves the parametrization marks (we need them) return makefun.partial(f)
def test_so_partial(capsys): """ Tests that the answer at https://stackoverflow.com/a/55165541/7262247 is correct """ def foo(a, b, c=1): """Return (a+b)*c.""" return (a + b) * c bar10_p = partial(foo, b=10) assert bar10_p(0) == 10 assert bar10_p(0, c=2) == 20 help(bar10_p) captured = capsys.readouterr() with capsys.disabled(): print(captured.out) assert captured.out == """Help on function foo in module makefun.tests.test_so:
def __get__(self, obj: Subscriber, objtype=None) -> Callable: # This returns the method that was wrapped return partial(self.func, obj)
def __get__(self, obj: CommandRunner, objtype=None): return partial(self.func, obj)