Python에는 일련의 내장 메소드가 있으며__call__>그 중 하나입니다. 그만큼__call__>메서드를 사용하면 Python 프로그래머는 인스턴스가 함수처럼 동작하고 함수처럼 호출될 수 있는 클래스를 작성할 수 있습니다. 인스턴스가 함수로 호출되는 경우; 이 메소드가 정의된 경우x(arg1, arg2, ...)>은 약칭이다x.__call__(arg1, arg2, ...)>.
object() is shorthand for object.__call__()>
예시 1:
powershell 보다 작거나 같음
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()> |
자바 더블을 문자열로
>
김프 교체 색상
>
출력 :
Instance Created Instance is called via special method>
예시 2:
자바 테일
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>)> |
>
부울을 문자열로 변환 Java
>
출력 :
Instance Created 200>