Experiences Switching to Python
We love most of Python, especially the clean, readable, minimal syntax. However, there are several Python gotchas, and we were bitten by almost all of them. Here are some of the major ones that confused either students or instructors:
- There are no good GUI packages; Swing was at least teachable, but Tkinter has lots of warts and none of the others (e.g., wxPython, PyQt) seem to be usable on all three major OSes. We’ve dropped Tkinter this fall in favour of EasyGUI, which has major limitations but is much more teachable.
- It’s hard to know when to stop. List comprehensions, for example, are really cool and natural, but they make a surprising number of problems too easy and allow students to avoid writing loops. We show them toward the end of our intro course but don’t require the students to use them.
- Aliasing is surprising and comes up naturally and even accidentally, especially in lists. This is not necessarily Python-specific, but it sure comes up more in Python than it did in Java.
- Default values for parameters are created once. Students (and some instructors) hate this.
- Simple classes are harder to teach than in Java. Also, “self” confuses a large subset of the students. Since dictionaries are so accessible they do whatever they can to avoid using and understanding classes.
- Global scope is hard to explain: it’s actually module scope and not global, and in the three examples below there are different behaviours; the last example in particular really confuses students. They do all of these things naturally, so we can’t prevent them getting confused.
# Example 1 i = 1 def f(): print i f() print i# Example 2 i = 1 def g(): i = 2 print i g() print i# Example 3 i = 1 def h(): print i i = 2 print i h() print i- More scope: nested functions baffle students.
On the positive side, some students LOVE putting functions into dictionaries and getting rid of huge if/else statements. They also find passing functions as parameters to be natural; we introduce this idea to write a timing function that returns how long its argument runs. This is a lovely thing to do when we get to searching and sorting.