Init a table of functions

Richard Blackwood richardblackwood at cloudthunder.com
Sat Oct 9 22:05:27 EDT 2004


Well, I tried self and it said the same as with foo.  So I just called 
f1() and f2() without self or foo in front since they should be within 
scope and it worked to that extent but complained further, see below:

File 
"C:\Python23\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", 
line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "..\My Documents\CODE\test_dumpscript.py", line 20, in ?
    main()
  File "..\My Documents\CODE\test_dumpscript.py", line 16, in main
    foo.K["f1"]()
TypeError: 'staticmethod' object is not callable
 >>>


> How do I program something like the folowing program?
> PYTHON claims it doesn't know about 'foo' when initializing K!
> The only way I got this to work was doing the init of K in
> the __init__ func. But this runs everytime for each object!
> When using the name of the function without 'foo', I don't
> know how to call it!
>
> Thanks for any help.
>
> #! /bin/env python
> # -*- coding: iso-8859-15 -*-
>
> class foo:
>     def f1():
>         print "f1"
>     f1=staticmethod(f1)
>
>     def f2():
>         print "f2"
>     f2=staticmethod(f2)
>
>     K={"f1" : foo.f1, \
>        "f1" : foo.f2  \
>     }
>
>
> def main():
>     foo.K["f1"]()
>     foo.K["f2"]()
>
> if __name__ == "__main__":
>     main()





More information about the Python-list mailing list