Referencing Static Methods From Class Variable
I know it's wired to have such a case but somehow I have it: class foo #static method @staticmethod def test(): pass # class variable c = {'name' :
Solution 1:
classFoo:
# static method @staticmethoddeftest():
pass# class variable
c = {'name' : test }
Solution 2:
The problem is static methods in python are descriptor objects. So in the following code:
classFoo:
# static method @staticmethoddeftest():
pass# class variable
c = {'name' : test }
Foo.c['name']
is the descriptor object, thus is not callable. You would have to type Foo.c['name'].__get__(None, Foo)()
to correctly call test()
here. If you're unfamiliar with descriptors in python, have a look at the glossary, and there's plenty of docs on the web. Also, have a look at this thread, which seems to be close to your use-case.
To keep things simple, you could probably create that c
class attribute just outside of the class definition:
classFoo(object):
@staticmethoddeftest():
pass
Foo.c = {'name': Foo.test}
or, if you feel like it, dive in the documentation of __metaclass__
.
Post a Comment for "Referencing Static Methods From Class Variable"