Member-only story
Python’s “with as” Statement in Julia, JavaScript, Lua and Other Languages
One of the challenges for me to grasp the with as
feature in Python, its need is not apparent unless you have a deeper understanding of the limits of the Python language.
A Ruby, Julia, Lua, Go, JavaScript or Swift developer for instance would not need this statement added. Why is that?
At the heart of the problem is a cherished feature by many Python developers: the use of indentation to indicate code blocks.
In JavaScript I could write e.g.
openfile('foobar.txt', function(file) {
// called after file has been opened
// file is a handle to the open file
read_data(file)
...
});
Lua would support a similar pattern:
openfile("foobar.txt", function(file)
-- called after file has been opened
-- file is a handle to the open file
read_data(file)
...
end)
Now imagine what that would look like in Python:
openfile("foobar.txt", def (file):
# called after file has been opened
# file is a handle to the open file
read_data(file)
...
This would not work. It would not be possible for the compiler to figure out how things are nested. Should double indentation mean things are more deeply nested e.g.?
It illustrates the benefit of having a symbol to signal the end of a block of code rather than a change in indentation.