[Edu-sig] More on teaching about namespaces

kirby urner kirby.urner at gmail.com
Wed Sep 13 20:06:13 CEST 2006


What I like about namespaces is the idea is intuitively obvious to
anyone spending any time in academia, because every professor is one.
It takes time to learn just what each means by this or that key term,
although supposedly in math it's easier, because all the definitions
are agreed upon in advance, axioms too, plus rules of deduction, so
the theorems also.

My idea of a good first move, after booting some Python GUI shell for
the first time, is to go dir().

IDLE 1.2b2
>>> dir()
['__builtins__', '__doc__', '__name__']

Already the filling is:  we're not alone.  And what are those weird
__xxx__ things.  They look strange.  Thinking like a computer
scientist, you mentally pair tics and decide we have three quoted
words, like a list of some time.  See?  Python fits your brain.
You're already thinking like a pro.  Instead of words we say strings,
and a space is just one more character (ASCII 32).

But here comes the *big* revelation:

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError',
'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis',
'EnvironmentError', 'Exception', 'False', 'FloatingPointError',
'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
'None', 'NotImplemented', 'NotImplementedError', 'OSError',
'OverflowError', 'PendingDeprecationWarning', 'ReferenceError',
'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',
'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
'TabError', 'True', 'TypeError', 'UnboundLocalError',
'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
'UnicodeTranslateError', 'UserWarning', 'ValueError', 'Warning',
'WindowsError', 'ZeroDivisionError', '_', '__debug__', '__doc__',
'__import__', '__name__', 'abs', 'all', 'any', 'apply', 'basestring',
'bool', 'buffer', 'callable', 'chr', 'classmethod', 'cmp', 'coerce',
'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict',
'dir', 'divmod', 'enumerate', 'eval', 'execfile', 'exit', 'file',
'filter', 'float', 'frozenset', 'getattr', 'globals', 'hasattr',
'hash', 'help', 'hex', 'id', 'input', 'int', 'intern', 'isinstance',
'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'long',
'map', 'max', 'min', 'object', 'oct', 'open', 'ord', 'pow',
'property', 'quit', 'range', 'raw_input', 'reduce', 'reload', 'repr',
'reversed', 'round', 'set', 'setattr', 'slice', 'sorted',
'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'unichr',
'unicode', 'vars', 'xrange', 'zip']

Ohmygod omygod, what *is* going on now?  Obviously errors are a big
part of the Python experience (why else would that be the major topic
in like the whole first half of the list?), and then you have a bunch
of underunder guys (ribs?), and then some short words, including one
especially ugly one with an internal underbar.

Now, as the teacher, I've probably just been projecting so far,
encouraging students to all focus on the big screen up front, and the
bouncing laser pointer dot, which drifts over this stuff as I vocalize
about content.

So I'm the one that shows 'em they can now select from the above list
and go (for example):

>>> help(iter)
Help on built-in function iter in module __builtin__:

iter(...)
    iter(collection) -> iterator
    iter(callable, sentinel) -> iterator

    Get an iterator from an object.  In the first form, the argument must
    supply its own iterator, or be a sequence.
    In the second form, the callable is called until it returns the sentinel.

Aha.  Lots of jargon.  iterator, callable, argument, sequence...
Let's try another.

>>> help(max)
Help on built-in function max in module __builtin__:

max(...)
    max(iterable[, key=func]) -> value
    max(a, b, c, ...[, key=func]) -> value

    With a single iterable argument, return its largest item.
    With two or more arguments, return the largest argument.

Hmmmmm, overlapping jargon.  There's that iterable notion again.
Gotta learn that square brackets mean *optional* stuff, i.e. you don't
need it.

Is a bare list an iterable?

>>> max([1,2,3,4])
4

Looks like.

Let's explore some more....

>>> max([1,2,3,4])
4
>>> a = [1,2,3,4]
>>> b = iter(a)
>>> id(a)
23472728
>>> id(b)
23397744
>>> type(a)
<type 'list'>
>>> type(b)
<type 'listiterator'>
>>> max(b)
4

and so on...

This might be more for adults with some programming experience
already, but not much with Python.

But you see how I'm bringing the namespace idea in right away.
Immediately upon being in the shell, we're already in one.  Best to
look around.  dir() and help() are your friends.  Later, we'll import.
 But already, we're in a huge fish tank, lots to see and do.  Could
take an hour at least, to get through this aquarium -- very
interactive, so now I'll turn off the projector (actually, I'll leave
what you see).

Go crazy, play, explore.  We'll start up with the formal talk in about
15 minutes, and answer any questions.  Bathroom is down the hall to
your right.

Kirby


More information about the Edu-sig mailing list