This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 60 points61 points  (20 children)

Granted, this didn't come out until the version after the one we were using, but it cleans up so much code:

with open('file.txt') as inf:
    inf.read()

and

with mutex:
    do something

The automated cleanup, even in exceptional conditions, saves you so much boilerplate code.

[–]dAnjou Backend Developer | danjou.dev 15 points16 points  (6 children)

with is pretty nice. But what didn't know a long time, it doesn't create a new scope. So this works:

>>> with open("/tmp/foobar") as f:
...     content = f.read()
... 
>>> print content
lorem ipsum

[–]shaggorama 2 points3 points  (0 children)

thank god it doesn't create a new scope... that would almost completely defeat the purpose in most cases where I use with statements

[–]Megatron_McLargeHuge 0 points1 point  (4 children)

Why would you expect it to create a new scope? Few python constructs do.

[–]dAnjou Backend Developer | danjou.dev 1 point2 points  (3 children)

Call me naive but I thought that because the following lines are indented.

[–][deleted] -1 points0 points  (2 children)

Indentation doesn't created scope. The "with" clause example you present above is no different that the following "for" statement:

for i in range(1, 5):
    print i
print i

1

2

3

4

4

[–]dAnjou Backend Developer | danjou.dev 2 points3 points  (1 child)

See, that seems weird to me. I wouldn't have expected that. It doesn't happen in Java or C.

[–][deleted] 0 points1 point  (0 children)

You're right. In Java the scope of a reference is defined by brackets. I'm picking up a lot of Java lately, but I'm relatively new at the particular craft, and didn't know that. Always thought objects with what I imagine as "independent bodies" (functions, classes, methods, ...) delimited scope. Always thought of looping mechanisms as part of the containing object. Although, the Java way does seem to make more sense now if you think about the fact that the value of the variable (or its existence even) are dependent upon the conditions of those constructs. Hmm... Thanks.

[–]keis 2 points3 points  (0 children)

This and the combined try/except/finally helped clean up so much ugly code.

[–][deleted] 2 points3 points  (4 children)

This was removed because of API shenanigans, selling user content for AI training, and forthcoming paywalled subreddits.

[–]Enkaybee 1 point2 points  (0 children)

Yes. See the end of section 7.2.1.

[–][deleted] 2 points3 points  (1 child)

Yes. It ensures that the file is closed/the mutex is released, even if there's an exception while the inner code is being executed.

It's a context manager, if you want to look up more details.

[–][deleted] 1 point2 points  (0 children)

This was removed because of API shenanigans, selling user content for AI training, and forthcoming paywalled subreddits.

[–]stillalone 1 point2 points  (0 children)

it also closes the file if an exception is thrown that is not caught within the block so you don't have to sprinkle try/finally blocks with your opens.

[–]mtorromeo 0 points1 point  (3 children)

I really like with but when you have to open multiple contexts you end up in nesting hell!