I have a keyword argument function:
def f1(**kw):
for key,val in kw.iteritems():
print "key=%s val=%s" % (key,val)
f1(Attr1 = "Val1", Attr2 = "Val2") # works fine.
f1(Attr1-SubAttr = "Val1", Attr2 = "Val2") # complains about keyword being an expression.
f1("Attr1-SubAttr" = "Val1", Attr2 = "Val2") # doesn't work either.
How do I pass in keywords with a hyphen? I don't have control over these keywords since I am parsing these from an existing legacy database.
Thanks! -kumar
Hyphens Are Word Boundaries on the Web git repository names. Python packages, specifically those in PyPI (the main repository for pip )
Arbitrary Keyword Arguments, **kwargsIf you do not know how many keyword arguments that will be passed into your function, add two asterisk: ** before the parameter name in the function definition.
If you need to write a function that accepts arbitrary keyword arguments, you can use the ** operator in your function definition.
Keyword arguments must be valid Python identifiers; these don't allow for -
as that's reserved for subtraction.
You can pass in arbitrary strings using the **kwargs
variable keyword argument syntax instead:
f1(**{"Attr1-SubAttr": "Val1", "Attr2": "Val2"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With