how to erase a variable

G

greenflame

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. 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.
 
J

John Machin

greenflame said:
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. 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.

del rubbish, garbage, junk, unwanted_stuff
 
B

Ben Finney

greenflame said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top