In Python 3, I don't required to teach followings to newbies. 1. Don't do `class Foo:`, do `class Foo(object):`. 2. Don't do `isinstance(x, int)`, do `isinstance(x, (int, long))`. 3. Don't return non-ASCII string from `__repr__`, otherwise UnicodeError happens in logging and you will lost your important log. 4. Use %r instead of %s in logging to avoid UnicodeError when __str__ returns non ASCII strings.
I think there are many pitfalls fixed in Python 3 other than above. Python 3 is far easier to teach and review code than Python 2. On Thu, Feb 18, 2016 at 4:27 PM, Chris Angelico <ros...@gmail.com> wrote: > On Thu, Feb 18, 2016 at 5:47 PM, Steven D'Aprano > <steve+comp.lang.pyt...@pearwood.info> wrote: > > There are more features in Python 3, so in that trivial sense of "more to > > learn", I suppose that it is objectively correct that it is harder to > learn > > than Python 2. But I don't think the learning curve is any steeper. If > > anything, the learning curve is ever-so-slightly less steep. > > Let's see... changes in Py3. > > 1) Division of two integers now yields a float instead of flooring. > For someone fresh to programming, that's a Py3 advantage, although it > can cause surprises elsewhere. But since 1.0==1, it's not going to be > a problem for a new programmer. Advantage: Py3. > > 2) Strings are Unicode text, and files etc may need to have their > encodings declared. Definitely causes some issues in ASCII-only > situations, where a lot of other languages (notably including PHP, for > the people building web sites) let you be sloppy. Advantage: Py3 if > you speak any language other than English; otherwise Py2 in the very > short term, neither in the medium term, and most definitely Py3 in the > long term (no more "funny characters break my program" errors long > after deployment). > > 3) Laziness. When you explain to someone what the range() function > does, Py2 makes a list, but Py3 makes... a range. It doesn't really > answer the question at all. When you ask Py2 for a dictionary's > keys/values, you get a list; Py3 gives you a thing that mostly acts > like a list, only it isn't. If you map a function over a list, you get > back a lazy thing that will eventually call that function. Py2 often > has less levels of indirection, ergo less things to try to explain. > Advantage: Py2; the benefits (lower memory usage, etc) aren't > significant to new users. > > 4) Exception chaining. You get more information when errors cascade. > Advantage: Py3, easily and without any question. > > 5) print statement/function. Py3 forces you to put parentheses on it, > which is no different from C's printf() or Pike's write() or any > number of other languages where console I/O needs no language support. > Maybe a tiny TINY advantage to Py2 in the short term, but as soon as > you introduce the less basic features, keyword arguments are way > better than the magic syntax the statement needs. (Also, trying to > explain the interaction between the print statement's "soft space" and > other console I/O is not easy.) By the time you've really learned the > language, the advantage belongs to Py3. > > 6) The fact that the name "python" may not invoke the interpreter you > want. Advantage: Py2, if any; there'll be times when they're on par, > but Py3 never comes out ahead. > > 7) Whether or not the interpreter comes pre-installed on your system. > As of a few years ago, that was a clear advantage to Py2 (most systems > would ship with both, or neither, or Py2 only), but that's shifting. > It's only a small difference, though; on Windows, you generally get > nothing, and on any system with a decent package manager, you should > be able to request either version with ease. > > It's actually a pretty tough call. Most of the Py3 advantages aren't > for the absolute beginner; it's not easier to write "Hello, world" in > Py3, and aside from the change to integer division, most of the > changes won't benefit small-to-medium scripts either. The biggest > advantage (Unicode by default) really only shows itself by sparing you > hassles later on - it's not going to make your life easier in the > short term, ergo it's not going to make the language easier to learn. > Py3 isn't so much easier as _better_. There are specific situations > where it's massively better, but for the most part, they're about on > par. > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list > -- INADA Naoki <songofaca...@gmail.com> -- https://mail.python.org/mailman/listinfo/python-list