Whither SmallScript? (was Re: Integer micro-benchmarks)

Piet van Oostrum piet at cs.uu.nl
Sat Apr 28 13:34:54 EDT 2001


>>>>> Johann Hibschman <johann at physics.berkeley.edu> (JH) writes:

JH> Steven D Majewski writes:
>>> class Foo < Bar
>>> def myMethod
>>> File.new ("test.dat", "w") { |f| 
>>> f.puts "Hello world!"
>>> }
>>> end
>>> end

>> What do the vertical bars around the f  ("|f|") above indicate ? 

JH> That indicates that the block takes one argument, which is named "f".

In other words, a lambda expression.

JH> In Python, this would be:

        In Python you could then also use the lambda expression.
        lambda f: f.puts("Hello World") or write rather than puts

JH> def File_new (filename, func_to_call):
JH>   try:
JH>     file_obj = open (filename, 'w')
JH>     func_to_call (file_obj)
JH>   finally:
JH>     file_obj.close()

JH> def block_function (f):   # i.e. this is what that |f| means...
JH>   f.write ("Hello, world!\n")

JH> File_new ("test.dat", block_function)

What about open("test.dat", "w").write ("Hello world")?
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list