Tuesday, June 28, 2011

Kwargs in Python

Recently I noticed that function cannot accept unicode strings as parameter names given in kwargs. They must be converted to strings.

I made a sample script
import sys

print "Version: %d.%d.%d\n" % sys.version_info[:3]

string_params = {'a':1, 'b': 2}
unicode_params = {u'a':1, u'b': 2}

def f(a,b):
 print "params: ", a, b

print "Run with string params:"
f(**string_params)

print "\nRun with unicode params:"
f(**unicode_params)

As output I got
Version: 2.5.1

Run with string params:
params:  1 2

Run with unicode params:
Traceback (most recent call last):
  Line 15, in 
    f(**unicode_params)
TypeError: f() keywords must be strings

To check out it online.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.