Forum Archive

Bug - turtle.py

PatoNegro

I am trying to learn Python to teach it to children. I am using the book “Python for Kids” by Jason Brigs. Chapter 4 introduces Drawing with TURTLES. The following code is entered in the Python console:
import turtle
t = turtle.pen()

Entering it in Pythonista that I purchased for my iPad Pro gave me the following error message.
Traceback (most recent call last):
File "", line 1, in
File "/var/containers/Bundle/Application/985F9C6D-5025-471A-AFF4-C482CEB52E0C/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/site-packages/turtle.py",
line 1203, in pen
pen.update(pen_dict)
NameError: name 'pen_dict' is not defined

Researching the forum I found the turtle2.py file - https://forum.omz-software.com/topic/5014/shape-method-for-turtle-py
Comparing the two files I found the following differences.
Pythonista turtle.py
1201 if pen is None:
1202 --- pen = {}
1203 pen.update(pen_dict)
Tries to update pen_dict even though it may not exist

Pythonista turtle2.py
1231 if pen is None:
1232 ---- pen = {}
1233 if not pen_dict is None: # Checks to see if pen_dict exists
1234 ---- pen.update(pen_dict)

I did some more research and found a complete turtle.py .
from: https://svn.python.org/projects/python/tags/r32/Lib/turtle.py
2396 if not (pen or pendict):
2397 ---- return _pd
2398 Use the isinstance command to determine if pen is a dictionary type.
2399 if isinstance(pen, dict):
2400 ---- p = pen
2401 else:
2402 ---- p = {}
2403 p.update(pendict)

SUMMARY:
Since there is a bug in the current Pythonista turtle.py file and since the turtle2.py file added shapes, I recommend that the turtle2.py be considered as a replacement/upgrade after verifying that it implements the other classes and functions of the current Pythonista turtle.py file.

Side-Note: I am a retired computer programmer with extensive OO experience, especially with VB.NET.

Update 2018_08_16 - - - Found my problem - - -
There are three lines in the first turtle example.
import turtle
t = turtle.pen()
t = forward(50)
The last line gave an error after fixing the pen.update(pen_dict) error because t did not have a forward() method.
Why? Well, the problem was simply the fact that I was calling the pen() function of the turtle module. After looking closely at the book, I realized that the code should be t = turtle.Pen(). That is, I needed to use an upper case P which meant that t was creating an instance of the Pen() class. When I corrected my mistake, the code ran from the console using the current Pythonista turtle.py module.

Ah, so much to learn about Python. However, I believe that the bug that I described above is real.

mikael

@PatoNegro, please see and potentially add to this issue.

PatoNegro

@mikael, thanks for the heads-up. I did not think of checking GitHub.

One new observation is that both turtle.py and turtle2.py for Pythonista use pen_dict as the variable name. The turtle.py file on svn.python.org uses pendict as the variable name. For now I am simply using the turtle2.py version for my class preparation.

JonB

note, you can always copy the turtle.py file to site-packages, and make whatever mods you want. for instance adding the shapes methods from the other thread, etc, or implementing a more complete implementation.

PatoNegro

@JonB, thanks. That is an interesting option. That option implies that files in site-packages take precedence over the production files. So much to learn about the Python language and the Python environment.

I am also trying to learn a little SWIFT to teach introductory concepts. For a starter I am using SWIFT Playgrounds on my iPad. I have XCode installed on my Mac Mini. However it seems more complicated than Python for teaching 4th to 6th grade students.

JonB
>>> sys.path
['/private/var/mobile/Containers/Shared/AppGroup/C534C622-2FDA-41F7-AE91-E3AAFE5FFC6B/Pythonista3/Documents/site-packages-3', '/private/var/mobile/Containers/Shared/AppGroup/C534C622-2FDA-41F7-AE91-E3AAFE5FFC6B/Pythonista3/Documents/site-packages', '/var/containers/Bundle/Application/DBC1896E-6227-4695-9B4B-AC1340AD4945/Pythonista3.app/Frameworks/Py3Kit.framework/pylib', '/var/containers/Bundle/Application/DBC1896E-6227-4695-9B4B-AC1340AD4945/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/site-packages', '/var/containers/Bundle/Application/DBC1896E-6227-4695-9B4B-AC1340AD4945/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/site-packages/PIL_compat', '/var/containers/Bundle/Application/DBC1896E-6227-4695-9B4B-AC1340AD4945/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/lib/python36.zip', '/var/containers/Bundle/Application/DBC1896E-6227-4695-9B4B-AC1340AD4945/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/lib/python3.6', '/var/containers/Bundle/Application/DBC1896E-6227-4695-9B4B-AC1340AD4945/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/lib/python3.6/lib-dynload']

which shows that site-packages-3 takes precedence over site-packages which takes precdence over the build in modules. (in the 2.7 interpreter, it uses site-packages-2 and site-packages) That is nice because you can update certain things, though you must also be careful you don't make a module with the same name as a builtin.

Some other info which might be useful, during startup, pythonista tries to import a module pythonista_startup which you could place in site-packages (applies to both interpreter) or site-packages-3. You could have your startup download weekly assignments or examples, print instructions, etc.