Exec inside a class method to call other class methods?

Chris Rebert clp at rebertia.com
Thu Dec 25 19:24:12 EST 2008


On Thu, Dec 25, 2008 at 4:16 PM, J. Clifford Dyer <jcd at sdf.lonestar.org> wrote:
<snip>
> and so forth.  Then your if/else chain can be pulled out to the place where you instantiate each field:
>
> if data_type=='address':
>    field=AddressDataField(data)
> elif data_type=='due_date':
>    field=DueDateDataField(data)
> else:
>    field = DataField(data)
>

And then you use a dictionary of data_type to class to get rid of the
if-else chain altogether:

type2klass = {'address' : AddressDataField, 'due_date' : DueDateDataField} #etc
Klass = type2klass.get(data_type, DataField) #use DataField as fallback default
field = Klass(data)

Merry Christmas,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list