script question
Scott David Daniels
Scott.Daniels at Acm.Org
Fri Apr 17 12:50:37 EDT 2009
gitulyar wrote:
> On Apr 17, 5:23 pm, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
>> Marco Mariani wrote:
>>> Piet van Oostrum wrote:
>>>> funclist = [func01, func02, func03, ... ]
>>>> for i in range(1,n):
>>>> funclist[i]()
>>>> Or myscript.funclist[i]() from another module.
>> ...
>> For example, you could do it like:
>> funclist = []
>> def _included(function):
>> funclist.append(function)
>> return function
>>
>> @_included
>> def func01():
>> ...
>> def call_all():
>> for function in funclist:
>> function()
>>
>> --Scott David Daniels
>> Scott.Dani... at Acm.Org
>
> This code does the same as '__all__' should, but anyway it's
> the ugly way, because you have to edit '__all__' every time
> you add/remove new functions to your module and if '__all__'
> is absent your code won't work.
>
> As for me the best solution is to use 'dir()':
>
> for func in dir(myscript):
> if match(func):
> getattr(myscript, func)()
Well, I prefer that the names of the functions not matter, (that
renaming a function not change the behavior of a program). And if
you are not going by name, how do you refactor the functions that
you do want to call (and lift common behavior to a function).
It really does come down to style, and I like to avoid using
function names as the way to discover functions. I put up with
the naming convention for testing, but I don't like it. The only
excuse I see is that there are often a huge number of tests, and
there _is_ a convention for such names as defined by the xUnit style.
If we were starting from scratch now, I'd argue for a @test decorator.
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list