I guess the subject sounds pretty stupid, so I'll show some code:
def foo(**kwargs):
# How can you detect the difference between (**{}) and ()?
pass
foo(**{})
foo()
Is there any way to detect inside of foo, how the method was called?
Update 1
Because there were some comments why you possible want to do something, I'll try to explain some background.
super(MyClass, self).foo(*args, **kwargs)
sucks - a lot of wasteful duplication. I want to write 'self.super()'. In this case, just call the super class and hand over all parameters that the calling method got as well. Works like a charm.
Now the problematic magic part:
I want to say 'self.super(something)' and in this case, only 'something' is passed to the super method. Works for most cases.
This is where it breaks:
def foo(self, fnord=42, *args, **kwargs):
self.super(*args, **kwargs)
So the super method should get the arguments that the calling method - however if *args
, **kwargs
are empty, currently the library can not detect this condition and passed all arguments including 'fnord'...
Of course I could use self.super.foo(*args, **kwargs)
as an alternative syntax but that's lame :-)
PS: Yes, I know p3k's super, but still not nice and it does not work with Python 2.x...
Update 2
Actually even Python's ast module removes the **{}
(ast.parse('foo(**{})')
) so it looks like this happens so early in the parsing process that you can not get this information later on...
So in the end I have either to give up on that specific problem (raising an AmbiguousSyntaxError) or to use text parsing as proposed by ~unutbu. Re-thinking my approach, the latter might actually feasable because I only need to know if it is self.super(\s*)
, self.super(\S+)
.