Does java have an equivalent to python __call__? -
just started learning python, i've come across __call__ method. understand when __init__ gets called , when __call__ gets called running simple example:
class constructor: def __init__(self, a): print "__init__ called" def __call__(self): print "__call__ called" def dummy(self): print "a dummy method called" print "creating x obj" x = constructor(1) print "calling x object" x() console output:
creating x obj __init__ called calling x object __call__ called my questions are:
- i come java background, know
__init__maps java constructor, java have equivalent__call__me understand better? - can give me simple example of when
__call__can useful?
__call__ equates overloading () operator on class. possible in c++ not java. allows 1 have function objects (sometimes called functors).
in python these termed callable objects.
in simple terms, allows treat object function.
the actual use such concepts beyond scope of answer there plenty of information around internet. example wikipedia.
Comments
Post a Comment