-
Notifications
You must be signed in to change notification settings - Fork 18
Added a section of text about using the with construct. #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -273,7 +273,25 @@ If you want to try all this out, here's a quick exercise to make sure you've got | |
|
||
When writing to the files, remember that `print()` adds a newline but with `write()` you have to add the newline yourself. | ||
|
||
Hopefully pretty simple, but that should make sure you have all the above ideas down-pat. | ||
Hopefully pretty simple, but you should make sure you have all the above ideas down-pat. | ||
|
||
### Python 2.x and 3.x | ||
|
||
It's useful to have seen everything above, but there is a preferred way to handle these intricacies. It uses a python reserved word: `with`. Unfortunately, python is in a transition period between 2.7 and 3.x, so there is an added complication. This tutorial is based on 2.7, while the future will be 3.x. The good news is, you can properly write code that will work in both versions by adding the following line to 2.7 code once (near the top of your script with other imports): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh, I know same-source multi-version compatibility is a hard topic but this discussion seems like it's boring and out of scope for the "data processing" discussion. Is there a way to make this easier, maybe by just proposing to add it by default (enable it, so to say) and then adding a hint/disclaimer for 3.x that you can leave it out? This seems pretty lengthy and boring right now. |
||
|
||
from __future__ import absolute_import, division, print_function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why exactly do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need There are two ways to go about this:
or
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're going to do one, there's almost no reason not to do the others (at least, if you're writing novel code). That way you're more systematically trying to be python 3 compatible, rather than selectively. |
||
|
||
If you are using version 3.0 (or a higher number), feel free to leave it out (though it doesn't hurt to leave it in, either). But, assuming you are using 2.7, see how to print an example file below: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if "print an example file" is language non-programmers would use/understand. Maybe "write" is a better fit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Except, in contrast to the section above, where you are actually using |
||
|
||
from __future__ import absolute_import, division, print_function | ||
# ... any other code you want here ... | ||
with open("extrafile.txt", "w") as file_handle: | ||
print("First line", file=file_handle) | ||
print("Second line", file=file_handle) | ||
print("Third line", file=file_handle) | ||
|
||
Notice a couple of things with this construct. First, the `with` opens a new code block. The nice thing about the way this is handled is that it will automatically close the file (even if an exception is raised). Second, we can use `print` to print (without a "\n" character specfied), but we have to tell `print` where the text is going (in this case the `file_handle`). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The causal link between "opens a new code block" and "automatically close the file" is not clear to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it more clear if reword it this way:
|
||
|
||
|
||
## Next Chapter | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is not from your PR, but while we're at it we could maybe remove the passive-aggressive tone in that? It implies you're somehow not fit as a learner to continue if you did not understand all of this by your heart.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1. I just jumped in and did this one as 4dc8aa9 (will probably cause a merge conflict, sorry!)