This should be very simple but I failed to google it: How (if at all) can I overload the parantheses operator in Python? So that this code will make sense:
my_new_object = my_new_class()
x = my_new_object(5)
You need to define __call__
on your class.
For example
>>> class Multiplier(object):
... def __init__(self, num):
... self.num = num
... def __call__(self, other):
... return self.num*other
...
>>> mul5 = Multiplier(5)
>>> mul5(6)
30
Define __call__()
on your class:
class MyNewClass(object):
def __call__(self, x):
return x
You should look at the "Callable types" section of this document . In particular, your class could implement __call__
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.