简体   繁体   中英

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?

升级到Python 2.6.5或更高版本。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM