At first glance, I did not know what yield do in Python, but after having a hard time studying it, I finally understand the idea. But having calling this = yield
and later passing the mysterious this.send
as callback, does not seem clear than right away defining some lambda or closure. However, you should ask this to a python beginning which do not know neither closures nor what yield does, because I am already used to lambdas and closures. Or perhaps I am not seeing the big picture because the example is too simple or I am too dumb, therefore in a more complex scenario this could have great advantages. Then you could add also a complex example using closures vs resumeback.
I got that linear order, however I do not see how that can easier than just use closures. My problem is against the declaration of the first this = yield
and the second yield function(this.send)
. If I look both codes:
- With
closure
def main():
arbitrary_value = 10
def on_done(number):
number = str(number)
print("Result:", number * arbitrary_value)
ask_for_user_input("Please enter a number", on_done)
- With
resumeback
from resumeback import send_self
@send_self
def main():
this = yield # "this" is now a reference to the just-created generator
arbitrary_value = 10
# Yield pauses execution until one of the generator methods is called,
# such as `.send`, which we provide as the callback parameter.
number = yield ask_for_user_input("Please enter a number", this.send)
number = str(number)
print("Result:", number * arbitrary_value)
I can clearly understand when I what is going on with closures. But when looking at those two yield
's with resumeback
, I do not see it working clearly. Basically, I would say that closures are more intuitive than the this = yied
plus the second yield function(this.send)
call.
I think it would help, if the first this = yield
call could not required, so I can forget about it. Hence, only the second yield
call could be done like this:
from resumeback import send_self
@send_self
def main():
arbitrary_value = 10
# Yield pauses execution until one of the generator methods is called,
# such as `.send`, which we provide as the callback parameter.
number = yield ask_for_user_input("Please enter a number", send_self)
number = str(number)
print("Result:", number * arbitrary_value)
Can the this = yield
call be embed inside the decorator @send_self
and formerly to "send myself"
I could just call yield function(send_self)
?
I would say that it would look much better than having to remember both to do the first call this = yield
and yield function(this.send)
.
@FichteFoll I could do this:
from resumeback import send_self
@send_self
def main(this):
# Yield pauses execution until one of the generator methods is called,
# such as `.send`, which we provide as the callback parameter.
number = yield ask_for_user_input("Please enter a number", this.send)
number = str(number)
print("Result:", number * arbitrary_value)
I'm unsure how I would make the above work for methods though. Gonna need even more magic
I would suggest the this
to be a function pointer, so, it can be more straight forward. However, I do not know about other usages of it where could not be pointing to a function directly:
from resumeback import send_self
@send_self
def main(myself):
arbitrary_value = 10
# Yield pauses execution until one of the generator methods is called,
# such as `.send`, which we provide as the callback parameter.
number = yield ask_for_user_input("Please enter a number", myself)
number = str(number)
print("Result:", number * arbitrary_value)
For classmethods, perhaps it could be a cumulative sum of decorators:
from resumeback import send_self
@send_self
@classmethod
def main(myself, cls):
arbitrary_value = 10
# Yield pauses execution until one of the generator methods is called,
# such as `.send`, which we provide as the callback parameter.
number = yield ask_for_user_input("Please enter a number", myself)
number = str(number)
print("Result:", number * arbitrary_value)
But I do not know whether or not you can patch stuff like that in Python.