Skip to content

Conversation

@Fourgot
Copy link
Contributor

@Fourgot Fourgot commented Jul 8, 2015

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 cites from __future__ import print_function for Python 2.6+, that import and dependency are to assure access to the more robust functionality available with the newer print() function. The older print statement 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.

Fourgot added 2 commits July 7, 2015 23:52
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
Copy link
Owner

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)

@certik
Copy link
Owner

certik commented Jul 8, 2015

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):

>>> print("a", "b")
('a', 'b')
>>> from __future__ import print_function
>>> print("a", "b")
a b

src/rosetta.rst Outdated
Copy link
Owner

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.

Copy link
Contributor Author

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?

@Fourgot
Copy link
Contributor Author

Fourgot commented Jul 8, 2015

the older Python 2.7 doesn't ignore the parentheses

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.

Fourgot added 3 commits July 8, 2015 10:48
See discussion on GitHub at parent repo
#31

Comma-separated, parentheses-enclosed strings are parsed differently by
Python 2 and Python 3.
Copy link
Owner

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:

  1. "Integer", 5, "and float", 5.5, "works fine."
  2. "Integer " + str(5) + " and float " + str(5.5) + "."
  3. "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.

Copy link
Contributor Author

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

@certik
Copy link
Owner

certik commented Jul 8, 2015

I left some more feedback. I am fine with just requiring Python 3.4, and mentioning that you need to do from __future__ import print_function if you use Python 2.7. (In fact, I think that's the way to go, to always assume the latest Python version, which is 3.4, and just provide workarounds for backwards compatibility and keep the workarounds simple.)

@Fourgot
Copy link
Contributor Author

Fourgot commented Jul 8, 2015

I am fine with just requiring Python 3.4

Ah, well that makes it all a bit easier, for sure! Great, I'll get some changes to you soon. Thanks for your patience!

@certik
Copy link
Owner

certik commented Jul 8, 2015

Excellent. Ping me once you push some patches in, I'll review it again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants