[Tutor] dictionary dispatch for object instance attributes question

Rich Krauter rmkrauter at yahoo.com
Wed Feb 16 00:09:56 CET 2005


Brian van den Broek wrote:
[snip text]
> <code>
> class A:
> 
>     def __init__(self):
> 
>     self.something = None
>     self.something_else = None
>     self.still_another_thing = None
>     
>     def update(self, data):
> 
>     for key in metadata_dict:
>         if data.startswith(key):
>                 exec('''self.%s = """%s"""''' %(metadata_dict[key],
>                      data[len(key):]))
>         # triple quotes as there may be quotes in metadata
>                 # values
>         break
> 
[snip usage example and additional text]

Brian,

You could use setattr(self,metadata_dict[key],data[len(key):]).

Rich


More information about the Tutor mailing list