kopia lustrzana https://github.com/peterhinch/micropython-samples
Fix issue #45 with functor example.
rodzic
d2929df1b4
commit
d953332583
|
@ -78,10 +78,13 @@ class MyFunctor:
|
||||||
|
|
||||||
def __call__(self, arg):
|
def __call__(self, arg):
|
||||||
print('In __call__', arg + self.state)
|
print('In __call__', arg + self.state)
|
||||||
|
return self
|
||||||
|
|
||||||
MyFunctor(42) # prints 'In __init__ 42'
|
MyFunctor(42) # prints 'In __init__ 42'
|
||||||
MyFunctor(5) # 'In __call__ 47'
|
MyFunctor(5) # 'In __call__ 47'
|
||||||
```
|
```
|
||||||
|
In both test cases the object returned is the functor instance.
|
||||||
|
|
||||||
A use case is in asynchronous programming. The constructor launches a
|
A use case is in asynchronous programming. The constructor launches a
|
||||||
continuously running task. Subsequent calls alter the behaviour of that task.
|
continuously running task. Subsequent calls alter the behaviour of that task.
|
||||||
The following simple example has the task waiting for a period which can be
|
The following simple example has the task waiting for a period which can be
|
||||||
|
|
|
@ -39,6 +39,7 @@ class MyFunctor:
|
||||||
|
|
||||||
def __call__(self, arg):
|
def __call__(self, arg):
|
||||||
print('In __call__', arg + self.state)
|
print('In __call__', arg + self.state)
|
||||||
|
return self
|
||||||
|
|
||||||
MyFunctor(42) # prints 'In __init__ 42'
|
MyFunctor(42) # prints 'In __init__ 42'
|
||||||
MyFunctor(5) # 'In __call__ 47'
|
MyFunctor(5) # 'In __call__ 47'
|
||||||
|
|
Ładowanie…
Reference in New Issue