Exec inside a class method to call other class methods?

David Stanek dstanek at dstanek.com
Fri Dec 26 01:35:48 CET 2008


You won't need the dictionary at all if each type has a parse method.

On 12/25/08, Chris Rebert <clp at rebertia.com> wrote:
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>

-- 
Sent from my mobile device

David
http://www.traceback.org



More information about the Python-list mailing list