Overload () operator in Python

后端 未结 1 1921
予麋鹿
予麋鹿 2020-12-13 09:18

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 () op

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 09:48

    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!
    

    0 讨论(0)
提交回复
热议问题