Init a table of functions

Robert Kern rkern at ucsd.edu
Sat Oct 9 22:46:28 EDT 2004


Paulo da Silva wrote:
> 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.

Move the table outside of the class definition

class foo(object):
     def f1():
         print 'f1'
     f1 = staticmethod(f1)

foo.K = {'f1': foo.f1,
         }

foo.K['f1']()

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the Python-list mailing list