Passing a dictionary to a function in python as keyword parameters

I'd like to call a function in python using a dictionary.

Here is some code:

d = dict(param='test')

def f(param):
    print param

f(d)

This prints {'param': 'test'} but I'd like it to just print test.

I'd like it to work similarly for more parameters:

d = dict(p1=1, p2=2)
def f2(p1,p2):
    print p1, p2
f2(d)

Is this possible?

Answers


Figured it out for myself in the end. It is simple, I was just missing the ** operator to unpack the dictionary

So my example becomes:

d = dict(p1=1, p2=2)
def f2(p1,p2):
    print p1, p2
f2(**d)

Need Your Help

Are OpenGL indices and locations the same thing for uniforms and vertex attributes?

opengl opengl-es opengl-es-2.0 opengl-3 opengl-4

In the OpenGL Reference Pages, some functions are marked as using uniform locations, while other functions are marked as using uniform indices. Are these the same thing?

iOS: stopping webViewDidStartLoad

ios

Instead of letting webView to load the webpage, I want to add a new viewController to my application and load the URL instead.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.