Init a table of functions

Richard Blackwood richardblackwood at cloudthunder.com
Sun Oct 10 00:20:34 EDT 2004


<SNIP>

>
> class foo:
>      def f1():
>           print "f1"
>
>      def f2():
>           print "f2"
>
>      K={"f1" : f1,
>         "f2" : f2,
>         }
>      f1 = staticmethod(f1)
>      f2 = staticmethod(f2)
>
> def main():
>      foo.K["f1"]()
>      foo.K["f2"]()
>
> if __name__ == "__main__":
>      main()
>
> The 'staticmethod' is a descriptor and only works in
> the context of a class.
>
> Functions get turned into methods only in the context
> of a class.
>
> So to use a function as a function, get it before it
> turns into a method or staticmethod.
>
>                 Andrew
>                 dalke at dalkescientific.com

Ingenious solution Andrew!  A true beauty. 



More information about the Python-list mailing list