Pointing to function from class
Hans Nowak
ivnowa at hvision.nl
Tue Oct 17 04:51:29 EDT 2000
On 17 Oct 00, Adam Clark wrote:
> Hi.
>
> I want something like this:
>
> def SomeFunc () :
> # doesn't take self and can be called from outside a class
>
> class Class :
> f = SomeFunc
>
> c = Class()
> c.f()
>
> I can't keep the class from trying to bind the function.
Here is one way to do it...
def somefunc():
print "I am a happy function"
class Foo:
def __init__(self):
self.f = somefunc
>>> foo = Foo()
>>> foo.f
<function somefunc at 00A53C0C>
>>> foo.f()
I am a happy function
This may not be exactly what you want though, because it binds f to the
instance, rather than the class.
Here's another way, but it's kinda clumsy:
class Bar:
funcs = [somefunc]
>>> bar = Bar()
>>> bar.funcs[0]
<function somefunc at 00A53C0C>
>>> bar.funcs[0]()
I am a happy function
HTH,
--Hans Nowak (ivnowa at hvision.nl)
Info Vision Europe BV
More information about the Python-list
mailing list