0

Is it possible to execute an instance of a class? I don't mean instantiate a class, or execute a method. I mean something like a sibling to __string__ that would return a function. Let's call it __func__.

class ClassFunc (object):
    def __func__ (self):
        print "I'm a class!"

>>> myCF = ClassFunc()
>>> myCF()
I'm a class!

2 Answers 2

9

Close. You want __call__().

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. Thank you! (I promise I did search Google for a while, but I couldn't find the answer)
3
>>> class ClassFunc (object):
        def __call__ (self):
            print "I'm a class!"


>>> myCF = ClassFunc()
>>> myCF()
I'm a class!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.