Exec inside a class method to call other class methods?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Dec 26 07:35:00 EST 2008


En Fri, 26 Dec 2008 03:59:25 -0200, Carl Banks <pavlovevidence at gmail.com>  
escribió:

> 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).

Yep. There is a simple solution that still nobody suggested: rename all of  
__parse_foo as _parse_foo (single leading underscore), then:

def parse(self, data, data_type)
    return getattr(self, "_parse_%s" % data_type)(data)

unless there are strong reasons to keep the __ names.

-- 
Gabriel Genellina




More information about the Python-list mailing list