Jump to content

so I realize we are working on C# but I was wondering if someone can help me out with what should...


G+_Jason Perry
 Share

Recommended Posts

so I realize we are working on C# but I was wondering if someone can help me out with what should be a simple python question.

 

I want to clear out the data in a list after I am done using it.

 

for Line in RawFileList

    RawFileList[Line] = ""

 

I am using RawFileList to store the raw data from a file to be processed and spit out into another file.

Link to comment
Share on other sites

It didn't "wipe out" anything. Variables are just names for things in Python--the things themselves exist independently. When you built up that list from a file, memory was allocated for all the values and the list made to point to them. Now the variable points to an empty list. The old list is still in memory, but since it has no name (no Python variable points to it) anymore, the garbage collector can reclaim that memory. Or it may decide not to until it needs the memory. That's what garbage collection is all about--you don't need to do the memory management yourself.

Link to comment
Share on other sites

Yes, it takes some getting used to, if you're coming from another language like C or Visual Basic. But this is really how Python works. If you have a list with things in it and you want to start over, the simplest way is to assign a new, empty list to that variable. (Like Lee said, it's just a name that refers to some object.)

 

Don't worry about cleaning stuff up, that's a job for the garbage collector.

Link to comment
Share on other sites

As others have said, you don't usually need to clear out things after you use them.  The garbage collector does that when the variable is no longer used, or when the variable is set to something else.

 

If you have some special need, such as wanting  to make sure data is no longer referenced, you can usually just set it to None.   Technically you can do:

 

del RawFileList

 

Which will cause an error to be signalled if your code uses the variable without giving it a new value.

Link to comment
Share on other sites

 Share

×
×
  • Create New...