script question

gitulyar Gitulyar at gmail.com
Fri Apr 17 16:50:33 CEST 2009


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.
>
> > Ehm, calling a bazillion things in the right order should be a
> > responsibility of the myscript module anyway.
>
> For example, you could do it like:
>
> myscript.py:
>      funclist = []
>
>      def _included(function):
>          funclist.append(function)
>          return function
>
>      @_included
>      def func01():
>          ...
>
>      @_included
>      def func02():
>          ...
>
>      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)()



More information about the Python-list mailing list