Quick actions

cmd+k|ctrl+k

Navigation

Languages

Functor Example

Snippet info

Language

Python

Visibility

public

Author

chad

Created

2016-04-23T05:44:29Z

Updated

2016-04-23T05:44:29Z


class Adder(object):
    def __init__(self, starting_sum=0.0):
        self._current_sum = starting_sum
        
    @property
    def current_sum(self):
        return self._current_sum
        
    def __call__(self, value):
        self._current_sum += value
    

functor = Adder(starting_sum=42)
print("After init", functor.current_sum)
functor(1)
print("After first call", functor.current_sum)
functor(3)
print("After second call", functor.current_sum)
INFO