[Tutor] when is a generator "smart?"

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Jun 3 16:22:50 CEST 2013


On 3 June 2013 03:50, Jim Mooney <cybervigilante at gmail.com> wrote:
> Py 2.7, Py 3.3
> raw_input, input

There are good reasons for this change. Marc already suggested a
solution for writing code that runs under both versions.

> print, print()

If you put "from __future__ import print_function" at the top of your
module you can use the same print function in Python 2.6/2.7 and 3.x.
I recommend doing this rather than bothering with the 2.x print
statement.

> int a / int b = integer division, int a / int b = normal division and
> // is int division

"from __future__ import division" makes 2.6/2.7 behave like 3.x here.
Note that the floor division operator '//' is available in all
versions since 2.2 and always has the same meaning.

Again there are good reasons for this change and I certainly approve of it.

> 'my %s ate my %' % ('dog','shoes'), 'my {0} ate my
> {1}'.format('dog','shoes') --backported

Both % and .format work in all versions since 2.6: there is no
difference in 3.x. There are no plans to remove % formatting so just
use whichever you like.

> range makes a list, range makes an iterator (xrange for an iterator in 2.7)

Similar to Marc's suggestion earlier:

try:
    range = xrange
except NameError:
    pass

The newer range is IMO better and should be preferred over the older one.

> sort() sorts in place, sorted() returns a list - actually, I think
> that's in both recent Pys.

That is the same in every Python since Python 2.4 (when sorted() was
introduced). The same thing happens for reverse and reversed:

>>> a = [1,2,3]
>>> a
[1, 2, 3]
>>> a.reverse()
>>> a
[3, 2, 1]
>>> reversed([1,2,3])  # reversed returns an iterator
<listreverseiterator object at 0x00CF1750>
>>> list(reversed([1,2,3])) # so we'll turn it into a list
[3, 2, 1]

> Tkinter is now tkinter - Lord knows why they bothered doing that

I think the expectation when planning the 2.x to 3.x transition was
that most people writing code for both simultaneously would use
automatic code translation. This kind of thing is easily fixed
automatically but that's overkill when writing small scripts. The
recommendation for someone learning Python was expected to be: just
pick a version and only use books/tutorials/documentation for that
version.

There are other differences between 2.x and 3.x that you have not
mentioned. It doesn't really affect my own scientific programming but
the biggest difference is in text handling. Python 3.x uses unicode
for all text by default and uses type safety to protect users from
mixing encoded bytes with unicode text. This is a big change and was,
I think, a big driving factor in the decision to introduce a backward
incompatible version transition for Python. Once that decision was
made it created the opportunity to make lots of other small backward
incompatible changes (e.g. Tkinter -> tkinter).


Oscar


More information about the Tutor mailing list