[Tutor] creating variables at runtime

Paul Sidorsky paulsid@shaw.ca
Thu, 24 Jan 2002 00:42:11 -0700


Kirby Urner wrote:

> >So that was my challenge:  how do you write a raw_input loop
> >function *in the shell* that creates new variables at the
> >top level?
> 
> And a parallel challenge would be:  how to create new top-level
> variables in a module, *when the raw_input loop is a function*.

Ah okay so you were indeed talking about mixing namespaces.  You're
right, that's much trickier, and usually more trouble than it's worth.

Your tangential remarks caught my eye though...

> People who come to Python from VB or C++ are more likely
> to think in terms of self-contained programs that either
> take arguments at the top, or prompt for a few inputs,
> then go.  But I come from a Visual FoxPro background which
> boots to a command window.  Half the time I'm just sitting
> at the command line, opening data tables, sorting, listing.
> Modifying structure.  If I need a utility, I write it
> quickly, then run it.  No need to exit the shell.  I bring
> this same psychology to Python.
> I go into all this because I think newcomers especially
> should appreciate the two modes.  I think sometimes people
> who program professionally get so used to whatever mode
> they use that they forget...

As it happens, I took a two-day course in S-PLUS (a statistical
programming language for those who don't know) a few weeks ago and we
learned by doing everything in the interpreter.  It was a very powerful
way to program.  Not once did we ever type code into an editor and save
it to a file and run it.  The environment even saved the whole
interpreter session so on the second day when we restarted the program
all of our variables from the first day were still there.

Sadly it never occured to me to try such a thing with Python.  Like you
said it's just not the way people tend to think of doing things with
most programming languages.

Heck, for a while I kept forgetting the Python interpreter even
existed!  (I started out with interactive BASIC interpreters, but C does
weird things to the mind...)  I use it quite often now but probably not
nearly as much as I should.

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/