2

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?

Felix Kling
820k181 gold badges1.1k silver badges1.2k bronze badges
asked Oct 15, 2010 at 19:44
3
  • 3
    It calls the .clear() function on itself. There are hundreds of potential reasons for this, we need a bit more context. Commented 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.... Commented Oct 15, 2010 at 19:47
  • That's not much better, and better to add information explicitly relevant to the question to the question. Commented Oct 15, 2010 at 19:58

3 Answers 3

6

That line calls the clear method on the current object. What the clear method actually does depends on what class this code is inside.

answered Oct 15, 2010 at 19:46
Sign up to request clarification or add additional context in comments.

Comments

1

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()

answered Oct 15, 2010 at 19:52

Comments

-1

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.

answered Oct 15, 2010 at 20:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.