I would like to do something along the lines of…
class MyClassA(object):
an_attr = AnotherClassB(do_something=MyClassA.hello)
@classmethod
def hello(cls):
return "Hello"
however it will tell me MyClassA is not defined when I try to run it.
an_attrmust be a class attribute- I can’t alter
AnotherClassB - I would prefer if
hello()remained a classmethod
Any ideas?
You just have to get the order right, so it’s defined before you use it:
Note that, because it’s in the namespace when we’re creating the class, we just refer to
hello, not toMyClassA.hello.EDIT: Sorry, this doesn’t actually do what I thought it did. It creates the class without an error, but
AnotherClassBonly gets a reference to the unbound classmethod, which can’t be called.