converting dict to object

John Machin sjmachin at lexicon.net
Sat Dec 2 20:00:15 EST 2006


Steven D'Aprano wrote:
> On Sat, 02 Dec 2006 12:16:24 -0800, John Machin wrote:
>
> > The OP might consider adding code to the __init__ method to check for
> > cases where the dictionary key is not a string containing a valid
> > Python identifier (not a keyword).
>
[snip]
> But if he's doing something like this:
>
> attributes = fetch_user_dict()
> # attribute names aren't known until runtime
> obj.__dict__.update(attributes)
> for key in attributes:
>     print getattr(obj, key)
>
> then it is also redundant to check for valid identifiers, since getattr()
> doesn't need them.

but getattr() needs strings.




More information about the Python-list mailing list