Loading select queries into objects

Alex Martelli aleaxit at yahoo.com
Wed Jun 20 02:40:05 EDT 2001


"Graham Ashton" <graham at coms.com> wrote in message
news:WyRX6.1453$h45.9142 at news.uk.colt.net...
    ...
>   {'col1': 234, 'col2': 'foo', 'some_col': 9473.0}
>
> No, my question would be how would I take the above dictionary and convert
> it into calls to a col1(), col2() and some_col() methods (in the current
> class).

Assuming you mean "on the current instance", there is absolutely no
need for eval & friends -- the following will do just fine:

    def applydict(self, adict):
        for name, value in adict.items():
            try: method = getattr(self, name)
            except AttributeError: print "Warning: no method",name
            else: method(value)


Alex






More information about the Python-list mailing list