how to erase a variable

Ben Finney bignose+hates-spam at benfinney.id.au
Sat Jun 3 00:59:15 EDT 2006


"greenflame" <alikakakhel at yahoo.com> writes:

> Is there a way to get rid of a variable as though it never existed? I
> know this sounds very basic but I have not come across any such
> methods.

You can use the 'del' statement to delete a name.

> Also is the fact that I will have a bunch of extra variables just
> haning around because my use for them is over a bad thing? I will
> likely have on the order of 10 to 50 or so of these for the
> particular program I an working on at the moment.

It doesn't cause the computer any grief to have extra objects; they'll
eventually be cleaned up by garbage collection anyway.

That said, it sounds like your program is not modular enough, which is
a burden on the *programmer* to need to keep so many items in their
head at once. It's far better to divide your program into discrete
functional units, and write each conceptual step as a function.

By dividing your program into functional modules (whether they be
functions, classes with methods, or separate program modules) each
unit of functionality has a limited scope, and the variables are local
and get cleaned up after the function ends. This greatly aids the
person reading the program: they have fewer things to concentrate on
at any point in your program.

-- 
 \     "For every complex problem, there is a solution that is simple, |
  `\                            neat, and wrong."  -- Henry L. Mencken |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list