in Ruby, a block argument works like this:
def foo_bar (&block)
block.(4)
end
foo_bar do |x|
puts x
puts x * 2
end
=begin
4
8
=end
I've seen the following equivalent in Python, but I find it quite unsatisfactory, because it requires defining the function and only then passing it as an argument:
def foo_bar(block):
block(4)
def callback(x):
print(x)
print(x * 2)
foo_bar(callback)
'''
4
8
'''
is there any alternative to it in Python, that doesn't require the function to be defined first?