Python Output Versus Ruby Profitability

In Ruby, the yield keyword is used to ensure that execution blocks are closed.

How is this keyword different in Python?

+5
source share
2 answers

In ruby, exit is a shortcut that is used to call an anonymous function. Ruby has special syntax for passing an anonymous function to a method; The syntax is known as block . Since the function has no name, you use the name to call the function:

 def do_stuff(val) puts "Started executing do_stuff" yield(val+3) yield(val+4) puts "Finshed executing do_stuff" end do_stuff(10) {|x| puts x+3} #<= This is a block, which is an anonymous function #that is passed as an additional argument to the #method do_stuff --output:-- Started executing do_stuff 16 17 Finshed executing do_stuff 

In python , when you see the output inside a function definition, it means the function is a generator . A generator is a special type of function that can be stopped in the middle of execution and restarted. Here is an example:

 def do_stuff(val): print("Started execution of do_stuff()") yield val + 3 print("Line after 'yield val + 3'") yield val + 4 print("Line after 'yield val + 4'") print("Finished executing do_stuff()") my_gen = do_stuff(10) val = next(my_gen) print("--received {} from generator".format(val)) 

output:

 Started execution of do_stuff() --received 13 from generator 

More code:

 val = next(my_gen) print("--received {} from generator".format(val)) 

output:

 Line after 'yield val + 3' --received 14 from generator 

From the output, you can see that yield results in a return; then execution is immediately suspended. When you call next () again in the generator, execution continues until the next yield statement is returned, which returns a value, then execution stops again.

+8
source

Ruby yield is used to control the rollback for blocking (for example, an anonymous function) to execute block statements and then return back to where the block was called.

With yield args you can pass arguments to a block, and with lvar = yield you can get the return and bind it to lvar after the control exits the block. This is a very general and consistent feature design in Ruby. And of course, you can apply this idea to iterate over collections.

While mostly in Python people use yield to facilitate efficient access to elements due to some collection, they focus on iteration once and generate an β€œon the fly” ever-called idea, which is the main use of yield in Python.

FYI is not exactly the difference between Python and Ruby on yield , at least in the way it is used. (Apparently, they are implemented in different ways, since for python yield creates a generator that does not run any code unless the iteration begins). For example, the yield method is used in the python contextmanager, in Ruby it is exactly the same.

 from contextlib import contextmanager @contextmanager def openfile(name, mode): f= open(name, mode) yield f f.close() with openfile('log.txt', 'r') as handle: for line in handle: print line 

here execute pass file descriptor with and execute c-operators exactly once , and then return to the file closing statement

+1
source

Source: https://habr.com/ru/post/1243899/


All Articles