executing list of methods (and collecting results)
Gerardo Herzig
gherzig at fmed.uba.ar
Thu Sep 20 19:26:46 EDT 2007
Hi all. Im in this situation: I want to perform several kind of
(validating) methods to a given value.
Lets say i have a class named Number, and the following methods:
is_really_a_number(),
is_even(),
is_greater_than_zero(),
and so on. All of them returning booleans.
I want the collect_validators() method is to execute any of the above
methods, and collect their names as items of a list (wich will be the
collect_validators() return value).
My first approach is:
[code]
def collect_validators(self):
v_dict = { 'is_really_a_number': is_really_a_number,
'is_even': is_even,
'is_greater_than_zero', is_greater_than_zero
}
for name, meth in v_dict.items():
result = meth()
if result: yield name
[/code]
I wondering if is this a good pattern to apply, i like the way it looks
like, at least to me it looks `natural', but...im calling every method
twice here? One in v_dict and again on the dict iteration?
Any suggestion will be great!
Thanks!
Gerardo
More information about the Python-list
mailing list