[Baypiggies] Assigning functions as class variables
AM
ams.fwd at gmail.com
Sat May 31 05:51:01 CEST 2014
Hi.
What I am trying to do is assign a function to a class variable and use
it. For e.g.:
def foo(a):
print a
class X(object):
@classmethod
def bar(cls, fn):
cls.fn = fn
@classmethod
def baz(cls, a):
cls.fn(a)
X.bar(foo)
X.baz(12)
If I run this I get:
TypeError: unbound method foo() must be called with X instance as first
argument (got int instance instead)
I sort of understand why allowing this might cause a bit of trouble as
fn could be an unbound method which receives X as the first argument,
however I don't quite understand this particular error.
Changing foo to accept another argument also does not work. If someone
could enlighten me about:
1. Why exactly is calling the method disallowed in pretty much all forms?
2. How do people work around this?
that would be great.
Thanks.
AM
More information about the Baypiggies
mailing list