Alex Rivera | Logout

Passing dict to constructor?

Asked 2011-03-18T16:42:04.740
19

I'd like to pass a dict to an object's constructor for use as kwargs.

Obviously:

foo = SomeClass(mydict)

Simply passes a single argument, rather than the dict's contents. Alas:

foo = SomeClass(kwargs=mydict)

Which seems more sensible doesn't work either. What am I missing?

Edit
Report

1 Answer

3

To pass a dictionary to a constructor you have to do so by reference, which is by preceding it with **, like so:

foo = SomeClass(**mydict)
answered 2011-03-18T16:44:30.143

Your Answer