Self in Interactive Interpreter

kj7ny kj7ny at nakore.com
Fri Apr 4 16:20:55 EDT 2008


Hope this hasn't been posted hundreds of times. I have never seen it
before, but could have missed it.

For years it has been a slight annoyance that every time I wanted to
test a snippet of code from a class by running it in the interactive
interpreter, I had to remove all of the self. instances from the
code.  After I got it working correctly, I had to put all the self.'s
back into the code to put it back into my class.

The other day my brain had a functional synapse and I realized I could
just start my interactive session with:

>>> class dummy:
>>>     def __init__(self):
>>>         pass

>>> self=dummy()

I could then just set up my test variables something like:

>>> self.x='Hello'
>>> self.y='World'

And I could then use class type code such as:

>>> print self.x, self.y

I no longer had to remove and then replace all of the self. notation
from my code snippets.

So far I haven't seen why I shouldn't to this.  I haven't had any
problems using it, but I haven't used it that much.  If there is a
good reason to NOT do it, let me know.  Otherwise I hope this helps
anyone who has been annoyed by the same thing.

Thanks,



More information about the Python-list mailing list