[Tkinter-discuss] Moving object on page leaves afterimage on Python 3 but not on 2

Michael Lange klappnase at web.de
Fri Feb 5 11:03:50 EST 2016


I'm glad you finally got it working!

On Fri, 5 Feb 2016 16:56:58 +0200
Reinis Danne <rei4dan at gmail.com> wrote:

> Replacing this:
> map(self.paper.delete, items)
> with this:
> for i in items:
>     self.paper.delete(i)
> fixes the problem.
> I don't understand why the difference. That code is equivalent in
> both cases as far as I know. Is there a bug or I'm using map()
> incorrectly?

I'm not sure about this, the behavior of map() seems to have changed in
Python3, I just tried the following with Python2 first:

$ python
Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> l = ['a', 'b', 'c']
>>> def test(x):
...     print(x)
>>> map(test, l)
[None, None, None]

and then with Python3:

$ python3
Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> l = ['a', 'b', 'c']
>>> def test(x):
...     print(x)
>>> map(test, l)
<map object at 0x7fdfcc6342b0>

Personally I hardly ever use map(), so I never noticed that. In fact this
change is actually documented, see
https://docs.python.org/3.0/whatsnew/3.0.html#views-and-iterators-instead-of-lists :

"map() and filter() return iterators. If you really need a list, a quick
fix is e.g. list(map(...)), but a better fix is often to use a list
comprehension (especially when the original code uses lambda), or
rewriting the code so it doesn’t need a list at all. Particularly tricky
is map() invoked for the side effects of the function; the correct
transformation is to use a regular for loop "

Not sure what's the use of these map objects, in Python3 now you can call
the map object's __next__() method to iterate.
But for your purpose certainly the for loop is the way to go.

Best regards


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Intuition, however illogical, is recognized as a command prerogative.
		-- Kirk, "Obsession", stardate 3620.7

More information about the Tkinter-discuss mailing list