Class initialization from a dictionary, how best?

Nick Coghlan ncoghlan at iinet.net.au
Fri Jan 14 02:57:31 EST 2005


brianobush at gmail.com wrote:
> t2 = Test(dictionary.get('a'), dictionary.get('b'),
> dictionary.get('c'))
> print t2

Try this:

t2 = Test(**dictionary)

This performs keyword argument expansion on the dictionary, matching the 
dictionary entries with the named arguments to the Test.__init__ function.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list