× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



On Wed, Jul 15, 2015 at 11:14 AM, Jon Paris <jon.paris@xxxxxxxxxxxxxx> wrote:
Thanks for the offer. Similar changes have occurred in PHP but the transition was more gradual with a lot of backward compatibility - or so it seems to me. In 5+ years I think I have only encountered about 5 things that required real thought to refactor.

The transition from Python 2 to 3 is arguably bigger than any of the
PHP "major version" changes that have come to fruition. Python 3 is
roughly what PHP 6 was supposed to be. Both had as their centerpiece
the conversion of their string underpinnings to Unicode. Unlike PHP 6,
Python 3 actually happened.

No breaking changes for Python have ever been done before (so if you
wrote something for Python 1.5.2, it should still work as-is in Python
2.7.9), and as you might expect, no further breaking changes are
planned for the future. ;)

Here’s one example of a 2to3 issue. Original lines are prefixed - converted are + (I know you know that but others will be reading!)

- except IOError, (strerror, errno):
- print 'Error: Could not open file to read --->', self._ifile
+ except IOError as xxx_todo_changeme:
+ (strerror, errno) = xxx_todo_changeme.args
+ print('Error: Could not open file to read --->', self._ifile)
sys.exit(3)

It is the “xxx_todo_changeme” bits that confuse. Am I supposed to just give it a name of my choosing ? or ?

You really just dove in, didn't you? You didn't like my recommendation
of starting with the basics and moving up? (Specifically, working
through the official tutorial.)

I have to ask again: Where are you seeing this?

The Python 2 code you're starting with there isn't very good. What
it's trying to say is that IOError is supposed to come in two pieces,
and they want the first piece to be assigned to the variable
`strerror` and the second piece to `errno`. Now, that's a very
old-school exception syntax, even for Python 2. On top of that, the
first piece of the IOError is a number and the second piece is a
string. So just bad all around.

The short answer is: yes, just change `xxx_todo_changeme` to whatever
you like. But (1) if you want to keep the error number and the error
message string around, you'll want to reverse `strerror` and `errno`
for sanity; and (2) I don't see either of those actually being used,
so you could just as well get rid of them entirely. (Of course I can't
see past the snippet you've quoted, but it strikes me as unlikely that
those two variables matter, since the very next thing that happens is
exiting. Plus, if they did matter, one would expect that having
`strerror` and `errno` mixed up would have manifested in a noticeable
way at some point.)

So the "more correct" answer is: the Python 2 code probably should
have looked like this in the first place:

except IOError:
print 'Error: Could not open file to read --->', self._ifile

and then the Python 3 code would simply add the parens for print:

except IOError:
print('Error: Could not open file to read --->', self._ifile)

which 2to3 could have fixed on its own.

It's really frustrating to see beginners struggling with code that is
written by folks who are most likely not experts in the language being
learned. (The code Buck shared was also not written by a Python
expert. It was serviceable, but definitely not idiomatic.)

There were even just blatant errors in an IT Jungle article meant to
be an introduction to iSeriesPython (which was at 2.5 at the time).
With the blind leading the blind, someone is bound to get hurt. :(

John Y.

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.