Python function with **kwargs Question

Khoa Nguyen khoa.coffee at gmail.com
Thu Jan 5 17:41:13 EST 2006


Thanks for your responses. I guess the foo(**{'x-y':3}) is ugly but will do
the trick

Cheers,
Khoa

On 1/5/06, Mike C. Fletcher <mcfletch at rogers.com> wrote:
>
> Christian Tismer wrote:
>
> >Khoa Nguyen wrote:
> >
> >
> >>I would like to pass some keyword with special character to a
> >>foo(**kwargs) function, but it doesn't work
> >>
> >>def foo(**kwargs):
> >>  print kwargs
> >>
> >>
> >>This doesn't work:
> >>
> >>foo(a-special-keyword=5)
> >>
> >>How do I tell Python to treat '-' as a normal character but not part of
> >>an expression?
> >>
> >>
> >
> >By changing the parser :-)
> >
> >
> Oh, you py-py guys, always thinking you have to re-implement Python ;)
>
> >Keywords are limited to obey Python syntax.
> >
> >
> Sure, but you can do something like this:
>
> >>> def x( **named ):
> ...     print named
> ...
> >>> x( **{'some-var-with- weird chars': True } )
> {'some-var-with- weird chars': True}
>
> That is, for the OP (who is unlikely to rewrite the parser), the
> solution could be as simple as treating the keyword as a piece of data
> and applying it to the function.
>
> Have fun,
> Mike
>
> --
> ________________________________________________
>   Mike C. Fletcher
>   Designer, VR Plumber, Coder
>   http://www.vrplumber.com
>   http://blog.vrplumber.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060105/877a7edc/attachment.html>


More information about the Python-list mailing list