ivallesp
ivallesp

Reputation: 2222

How to pass keyword arguments with illegal names to a function accepting **kwargs

Can you help me hack this? The context is a bit complex to explain, but tl;dr I have to deal with a production system where I cannot install libraries and one of the libraries expects illegal keyword arguments as input.

def hack_me(**kwargs):
   if kwargs.get("hack:/me") is not None:
      print("Hacked!")

The objective is that the function above, without modifying it, prints Hacked!.

Upvotes: 1

Views: 166

Answers (1)

MatsLindh
MatsLindh

Reputation: 52862

While regular arguments can't have any form for their name, expanded arguments using the same syntax as in your function signature (i.e. **args) can.

>>> args = {'hack:/me': True}
>>> hack_me(**args)
Hacked!

Upvotes: 6

Related Questions