Writing a generator that can be called like a regular function?
Hi there,
I have a function in a script that takes an object and returns a boolean depending on it's argument:
def test(person):
return person.name == 'bob'
However, sometimes I might want to edit it to return true every nth call, regardless of the person. So maybe a generator?
def test(n):
i=0
while True:
yield i%n==0
i+=1
However, then (I believe) I have to modify the code where it's called to use the next method. I tried encapsulating the generator function within another function, but then the generator returns it's first value every call.
Is this even possible? Maybe there's a solution using decorators?
Posted Jun 16, 2017, 7:12 PM UTC
65