milibopp / obsub

Small python module that implements the observer pattern via a decorator.
Other
12 stars 3 forks source link

**kwargs can not take `self` and `instance` #47

Open coldfix opened 10 years ago

coldfix commented 10 years ago

Consider the following example:

from obsub import event

class Foo(object):

    @event
    def bar(this, self):
        pass

    @event
    def baz(self, instance):
        pass

foo = Foo()

foo.bar(1)                  # works fine
foo.bar(self=1)             # TypeError

Foo.baz(foo, 1)             # works fine
Foo.baz(foo, instance=1)    # TypeError

The reason for this behaviour is obvious: obsub uses normal python arguments named instance and self in the wrapper functions, because python syntax doesn't allow for positional-only parameters. The fix is easy: make the signature of those functions (*args, **kwargs) and extract the self/instance parameter as the first parameter from *args.