How to store a function pointer in class?

Justin Sheehy dworkin at ccs.neu.edu
Thu Jan 20 15:34:41 EST 2000


"Sami Hangaslammi" <shang.remove_edu at st.jyu.fi.edu> writes:

> So I wan't to store function references into class variables and
> retrieve them unchanged.

Oh.  I appear to have misunderstood your real goal here.

You could store the function inside a mutable class data member.

For instance:

>>> class Test:  
...   class_attribute = []

>>> def test_func(x):  
...     print "test_func called with",x  

>>> Test.class_attribute.append(test_func)

>>> x = Test.class_attribute[0]

>>> x(1)
test_func called with 1

Is that something like what you were looking for?

-Justin

 




More information about the Python-list mailing list