Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass unicode keywords to **kwargs

I was exception the following to work.

def foo(**kwargs):
    print kwargs
foo(**{'a':'b'})
foo(**{u'a':'b'})

Traceback (most recent call last): File "", line 1, in TypeError: m() keywords must be strings

Am I doing something wrong or I should I fix it?

like image 407
Julien Grenier Avatar asked Jan 04 '11 21:01

Julien Grenier


2 Answers

Upgrade to Python 2.6.5 or later.

like image 169
Josh Lee Avatar answered Nov 16 '22 01:11

Josh Lee


Upgrading wasn't an option for me so I'm calling this on dicts as needed--

def flatten_unicode_keys(d):
    for k in d:
        if isinstance(k, unicode):
            v = d[k]
            del d[k]
            d[str(k)] = v
like image 21
Reed Avatar answered Nov 16 '22 03:11

Reed