Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload () operator in Python

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?

like image 539
jdyg Avatar asked Mar 30 '13 13:03

jdyg


People also ask

What is overload operator in Python?

The operator overloading in Python means provide extended meaning beyond their predefined operational meaning. Such as, we use the "+" operator for adding two integers as well as joining two strings or merging two lists. We can achieve this as the "+" operator is overloaded by the "int" class and "str" class.

Can we overload operators in Python?

Python does not limit operator overloading to arithmetic operators only. We can overload comparison operators as well.

What is operator overloading and overriding in Python?

1. In the method overloading, methods or functions must have the same name and different signatures. Whereas in the method overriding, methods or functions must have the same name and same signatures.


1 Answers

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! 
like image 89
Ned Batchelder Avatar answered Sep 29 '22 04:09

Ned Batchelder