Teaching (and learning) Python

Sunday 29 May 2011This is over 13 years old. Be careful.

I taught David Beazley’s 3-day Intro to Python class in Austin last week, and I really enjoyed it. I’ve always liked speaking in front of people (well, after the very first time, which was terrifying), and three days with a group of 15 made for a good intimate dynamic.

Interestingly, I learned a few tidbits about Python along the way:

  • The builtin enumerate() function has a two-argument form so that you can specify the first numbering value: enumerate(seq, start), handy for numbering lines in files, which should of course start with 1, not 0.
  • The -i switch to the Python interpreter will run your program, then leave you in the command prompt when it ends. This can be good for interactively testing the functions defined in the file, or for debugging if an exception happens.
  • If you use “python -i” and need to debug, pdb.pm() is the thing to use. It places you at the point where the last traceback was raised. It seems like magic, since the exception has already risen to the top level of the program, but pdb.pm() puts you “back in time” before the exception started its climb up through the stack.
  • 4 + True == 5. Useless, but there it is.

I liked explaining Python’s elegant power, and it’s fun to show experienced C programmers a different way of looking at programming. Trying to fit all the material into three days is a challenge, and there are interesting side-topics that I know we can’t get into, which is a shame, but there’s only so much you can do in a given amount of time.

Austin was a really fun city to explore on my own, too. If I had been more confident that I’d have energy left at the end of the day, I’d have tried to arrange some social events with other Python peeps. But the “Keep Austin Weird” vibe made it easy to wander around and find food trucks to eat at. My first night, driving down East 6th street, I saw a cluster of closed food trucks, with just one open, lit like a beacon at the back:

Pig Vicious

How could I not eat at a tiny truck named Pig Vicious manned by a giant tattooed bearded cook?

Comments

[gravatar]
"4 + True == 5" is not totally useless :P. If you have a set of elements and you want to count how many satisfy a given codition you can do:
how_many = sum(condition(x) for x in my_set)
[gravatar]
I recently discovered that bool is a subclass of int, which explains this unexpected behaviour. A design error IMO. I wonder if it's fixed in Python 3.
[gravatar]
> "4 + True == 5" is not totally useless :P. If you have a set of elements and you want to count how many satisfy a given codition you can do:

You could just as well do that using a filter:
how_many = len(filter(condition, my_set))
> I wonder if it's fixed in Python 3.

As far as I know, it is not considered broken by the core team.
[gravatar]
Martin,

True/False being a subclass of int is long-story-short a product of history. Before python 2.3 TF were undefined; they were added to builtins because it was very popular to define True = 1 and False = 0. Making them keywords would have disallowed assigning to those names and broken a lot of code.

In 3.x they _are_ keywords so you can't assign to them. They still are a subclass of integer though. Why? because it doesn't really hurt anything and makes the Objects/boolobject.c implementation just 184 lines long - versus 4889 lines for Objects/longobject.c
[gravatar]
It's amazing that True == 1 and False == 0.
You can have something like:
('case false', 'case true')[x>y]
to choose from a list without in the middle of string interpolation.

Filter is slow and deprecated since comprehensions became available. So the solution above using it is pretty bad.
filter(condition, list) is the same as [i for i in list if condition(i)].
But, instead of creating a new list, how_many = sum(condition(x) for x in my_set) uses an interator and saves memory.
[gravatar]
Ok, nerdlings, the whole reason Ned made the statement 4 + TRUE = 5 is useless was so that some poor soul would try to provide an example of its usefulness. Ned and the assorted Nerds who read his blog could then provide counterexamples of why the poor soul's example only re-enforces the original premise. They would then argue endlessly about the best way to dismiss poor soul's example. I'm only surprised Ned has not chimed in. Susan probably is making him mow the lawn or something. The rest of you, go out in the fresh air!
P.S. Ned - how the heck are you? how goes the solo gig?
[gravatar]
Kudla: "Ok, nerdlings, the whole reason Ned made the statement 4 + TRUE = 5 is useless was so that some poor soul would try to provide an example of its usefulness."

That's 4 + True, not 4 + TRUE.

(Posted at midnight on a Saturday night. Go figure.)

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.