0

I'd like to have attribute of class, which stores some function, but this function is not method of the class, it will be called later by other component completely out of scope of the class itself, so class is just container for the attribute with function. But call of this function fails with TypeError: unbound method

def f(x): print x
class A:
    not_method = f
A.not_method(10)

Is there any way to suppress this error? The obvious way is to store function warped by some data object, like tuple, but it seems not so elegant.

def f(x): print x
class A:
    not_method = (f,)
A.not_method[0](10)

Proposed solution with @staticmethod is not so cool, cause: - formally the attribute is not a method at all, it's just attribute with some external function - @staticmethod decorator does not work with attribute assignment

4
  • Can you explain your use case better? Sounds like you need to define a static method. Commented May 17, 2018 at 18:08
  • Do you just want to suppress that error ? Commented May 17, 2018 at 18:08
  • Which Python version is it? It works perfectly fine in Python 3-x. Commented May 17, 2018 at 18:09
  • 1) No, it's not the method at all, formally speaking, it's just link to some function which will be called completely out of scope of the class, so should be considered as data from class standpoint. 2) Yes, just supress error 3) Python 2.7, if it works in 3.0 it's ok, i'll just leave workaround for 2.7 and that's all. Commented May 17, 2018 at 18:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.