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

Bhaskar Chaudhary bha100710 at gmail.com
Fri Feb 5 11:33:51 EST 2016


Hi Reinis Danne

I am not sure if this is exactly the cause of your problem, but
map() returns a list in Python 2 but an iterator  in Python 3.

You can place a list around map() like:
   list(map(self.paper.delete, items))
to ensure that the result is still a list and has the same behavior in
both Python 2 and 3.

Another difference is that in Python 2 map() continues until the items
in the longest of the iterator is exhausted, extending the other
arguments with None. In Python 3 this is not the case, a similar
behavior can be achieved using itertools.zip_longest.

So I guess, your problem is related to one of the above two issues.

BTW, really nice program there you have built.

regards
Bhaskar

On 2/5/16, Michael Lange <klappnase at web.de> wrote:
> Hi,
>
> 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)
> a
> b
> c
> [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
>
> Michael
>
>
> .-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.
>
> Intuition, however illogical, is recognized as a command prerogative.
> 		-- Kirk, "Obsession", stardate 3620.7
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> https://mail.python.org/mailman/listinfo/tkinter-discuss
>


More information about the Tkinter-discuss mailing list