57

I am trying to learn currying in Python for my class and I have to overload the () operator for it. However, I do not understand how can I can go about overloading the () operator. Can you explain the logic behind overloading the parentheses? Should I overload first ( and then ) or can I do any of these? Also, is there special name for parentheses operator?

1

1 Answer 1

84

You can make an object callable by implementing the __call__ method:

class FunctionLike(object):
    def __call__(self, a):
        print("I got called with {!r}!".format(a))

fn = FunctionLike()
fn(10)

# --> I got called with 10!
Sign up to request clarification or add additional context in comments.

3 Comments

Can I make basic operations with that callable object, such as addition?
You can provide almost all the language operations: docs.python.org/2/reference/datamodel.html#special-method-names
Is () a call operator that essentially calls the object in question with a certain argument (or absence thereof)?

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.