4 ms·
But I can do all that with python already? Your example would work this way too: def _(f): content = f.read() return sum(int(c) for c in c
by PurpleRamen 3y ago
But I can do all that with python already?
Your example would work this way too:
def _(f):
content = f.read()
return sum(int(c) for c in content.split(" "))
with(open("file.txt"), _)
- sodapopcan 3y agoI don't think they were saying you can't do it in Python, just offering what is arguably a nicer syntax, but of course that is subjective. The anonymous function version lets you write things in the order they happen and also "binds" the operations together, ie, someone can't accidentally add a completely unrelated line between the `def` and the `with`. It ultimately doesn't matter, people just get used to the conventions of their preferred languages and we all trudge on.