[Tutor] "Pointer" to a function? Storing a function as an object property? Passing arguments by value/by reference?
greg whittier
greg at thewhittiers.com
Fri Jan 16 14:56:37 CET 2009
On Fri, Jan 16, 2009 at 7:51 AM, Vicent <vginer at gmail.com> wrote:
>
> That "problem" has to contain, somehow, a property or element called "function" which, in fact, I would like it to be a function, or a "pointer" to a function.
In python, the name of a function is just a pointer to it. Try this
>>> def foo():
print "Hi!"
>>> class Problem:
def __init__(self,fun):
self.fun = fun
>>> p1 = Problem(foo)
>>> p2 = Problem(foo)
>>> foo
<function foo at 0x012C52B0>
>>> p1.fun
<function foo at 0x012C52B0>
>>> p2.fun
<function foo at 0x012C52B0>
>>> p1.fun == p2.fun
True
>>>
More information about the Tutor
mailing list