-
Notifications
You must be signed in to change notification settings - Fork 18
Python printing #31
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
base: master
Are you sure you want to change the base?
Python printing #31
Conversation
Printing in Python 3 is now a proper function call, print(). This syntax is also recognized without issue in Python 2.
Abandons numpy.empty() due to its non-zero initial entries Switches a Python call from range(len(x)) to the current preference of enumerate(x) Expands some example arrays from range(1, 4) to range(1, 11) Finally moves Python function def calls to appear before module-scoped variables. I can't find a source to cite for this. I thought it was PEP8, but it's not. I've often seen Python modules laid out as imports, functions / classes, variables / "constants", evaluations.
src/rosetta.rst
Outdated
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.
The idea is correct, but a-a is ugly. Better to use zeros_like:
from numpy import array, zeros_like
b = zeros_like(a)
|
I agree to update the Python source code to the latest Python (3.4). However, I would point out that the older Python 2.7 doesn't ignore the parentheses, unless you import from future (that's probably what you meant): |
src/rosetta.rst
Outdated
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.
You should provide a Fortran equivalent as well.
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'm unfamiliar with Fortran, which is how I found your page. Is there something you could suggest?
Yeah, it looks like this is a case where the parentheses elicit different behavior between 2 and 3. Python 2 sees a tuple and Python 3 collects the non-keyword arguments and calls str() on them, joining them with the sep argument (defaults to You've successfully avoided discussing Python 2 versus 3, which is definitely outside the scope of this document. I suggest abandoning this syntax. I'll get a commit together shortly. |
See discussion on GitHub at parent repo #31 Comma-separated, parentheses-enclosed strings are parsed differently by Python 2 and Python 3.
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.
The order is now all mixed up. The point is to show side by side how to do:
"Integer", 5, "and float", 5.5, "works fine.""Integer " + str(5) + " and float " + str(5.5) + ".""Integer %d and float %f." % (5, 5.5)
People still use 3) in Python, even in Python 3.4. We can add the format way as well as 4), what corresponds to it is probably the same as the Fortran answer to 3).
But what is most important is to keep the order the same in both Python and Fortran, so that people can quickly learn how to format things in an equivalent way.
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.
Yep, totally mixed up now. I'll clean it up again lol
|
I left some more feedback. I am fine with just requiring Python 3.4, and mentioning that you need to do |
Ah, well that makes it all a bit easier, for sure! Great, I'll get some changes to you soon. Thanks for your patience! |
|
Excellent. Ping me once you push some patches in, I'll review it again. |
Mostly just adds parentheses around print statements, making them friendly to both Python 2 and Python 3. Note that while the documentation for
print()in Python 2 citesfrom __future__ import print_functionfor Python 2.6+, that import and dependency are to assure access to the more robust functionality available with the newerprint()function. The olderprintstatement would always ignore an enclosing set of parentheses surrounding its arguments, and for these simple calls should be acceptable in both 2 and 3.Additional edits are briefly described in the commit message.