Exec inside a class method to call other class methods?

Carl Banks pavlovevidence at gmail.com
Fri Dec 26 06:59:25 CET 2008


On Dec 25, 6:35 pm, "David Stanek" <dsta... at dstanek.com> wrote:
> You won't need the dictionary at all if each type has a parse method.

How do you know what type to use if you don't have a if...elif...,
dictionary, getattr, or some other sort of dispatching?  Look at his
requirements: there is a variable called "data_type" that controls how
to parse the data.  Whether he uses the variable to figure out what
method to call, or to figure out what type of object to create to call
the parse method of, some sort of dispatching is necessary.  This is
inevitable when type information is supplied through the input.

To be honest I am a little concerned over the more-or-less knee-jerk
suggestions to refactor this into a highly object-oriented approach.
For the task at hand the OP's approach is fine (aside from the use of
exec).


Carl Banks



More information about the Python-list mailing list