Klaus Neuner <klausneune...@googlemail.com> wrote:

> > handlers = {
> >     ".txt" : handle_txt,
> >     ".py" : handle_py,
> >     # etc
> >     }
> >
> 
> That is exactly what I would like to avoid: Having to map the function
> 'handle_txt' to '.txt'. Firstly, because I don't want to repeat
> anything and secondly, because I will one day add a new function and
> forget to add its name to the dictionary.

Use dictionary mantained by runtime:

def handle(extensions):
        funname = "handle_" + extension
        return globals()[funname]
        
handle('txt') # => function handle_txt

w.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to