I'm learning Python and ran into some code that has this line...
self.clear()
I am curious as to what it would do and why would someone need to do this?
-
3It calls the .clear() function on itself. There are hundreds of potential reasons for this, we need a bit more context.jkerian– jkerian2010年10月15日 19:46:04 +00:00Commented Oct 15, 2010 at 19:46
-
sorry about that....its basically called inside a function and the function looks like this __parse(self,filename)...so basically this function parses data from a file and returns an object subclassed from UserDict....kd.– kd.2010年10月15日 19:47:21 +00:00Commented Oct 15, 2010 at 19:47
-
That's not much better, and better to add information explicitly relevant to the question to the question.Nick T– Nick T2010年10月15日 19:58:45 +00:00Commented Oct 15, 2010 at 19:58
3 Answers 3
That line calls the clear method on the current object. What the clear method actually does depends on what class this code is inside.
Comments
If you found it inside the function, that looked like:
def __parse(self,filename):
then you will probably find something similar to this:
def clear(self):
If you find it, there's the code, that will be executed within self.clear()
Comments
sorry if it wasnt clear but i just found the code for clear() ... all it did was clear the UserDict object before assigning new values to it....i misunderstood the code and thought the writer was actually deleting the current object....anyway answer found....thanks for the help guys....i will be more careful in the future about how i word my questions and especially about what not to ask in the first place.