Parameterized Functions without Classes

Andrew Bennetts andrew-pythonlist at puzzling.org
Fri Jun 25 19:15:00 CEST 2004


On Fri, Jun 25, 2004 at 06:54:03PM +0200, Christian Tismer wrote:
[...]
> 
> In the ancient times, I saw myself writing classes
> to parameterize functions, like this:
> 
[...]
> 
> This can be easily done without an extra class, just by
> local variables which access the outer scope:
> 
> def _converter(scale):
>     def convert(arg):
>         return scale * arg
>     return convert

Note that even in "ancient times" (i.e. before nested scopes), you could
still do this without classes:

def _converter(scale):
    def convert(arg, scale=scale):
        return scale * arg
    return convert

-Andrew.





More information about the Python-list mailing list