Python ima nabor vgrajenih metod in__call__>je eden izmed njih. The__call__>omogoča programerjem Pythona pisanje razredov, kjer se primerki obnašajo kot funkcije in jih je mogoče poklicati kot funkcijo. Ko je primerek poklican kot funkcija; če je ta metoda definirana,x(arg1, arg2, ...)>je okrajšava zax.__call__(arg1, arg2, ...)>.
object() is shorthand for object.__call__()>
Primer 1:
linux ukazi ustvari mapo
class> Example:> >def> __init__(>self>):> >print>(>'Instance Created'>)> > ># Defining __call__ method> >def> __call__(>self>):> >print>(>'Instance is called via special method'>)> > # Instance created> e>=> Example()> > # __call__ method will be called> e()> |
>
>
Izhod:
Instance Created Instance is called via special method>
Primer 2:
nastavitev poti python
vzorci oblikovanja java
class> Product:> >def> __init__(>self>):> >print>(>'Instance Created'>)> > ># Defining __call__ method> >def> __call__(>self>, a, b):> >print>(a>*> b)> > # Instance created> ans>=> Product()> > # __call__ method will be called> ans(>10>,>20>)> |
>
>
Izhod:
Instance Created 200>