Self in Interactive Interpreter

K

kj7ny

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:

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

And I could then use class type code such as:

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,
 
F

Fredrik Lundh

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

wouldn't it be a lot easier to test your code by importing the module
containing it into the interactive interpreter?

or, shorter:

</F>
 
K

kj7ny

kj7nywrote:

wouldn't it be a lot easier to test your code by importing the module
containing it into the interactive interpreter?


or, shorter:


</F>

Didn't know about the

option. It makes sense now that I see it. Just hadn't ever tried it,
I guess. Thanks!

With some of my larger applications, it doesn't seem to work well to
try to run the whole thing in the interpreter. At least for me, I am
not a big IDE sort of programmer. I am much more comfortable in vim
and command line stuff. I suppose I should use the IDE more.

I wasn't offering this "tip" as a cure all. It was just an
observation should it be of use to someone out there. However, I
greatly appreciate your improvement on the example I gave. I will use
your class dummy: pass approach from now on.

Thanks,
 
F

Fredrik Lundh

kj7ny said:
With some of my larger applications, it doesn't seem to work well to
try to run the whole thing in the interpreter. At least for me, I am
not a big IDE sort of programmer. I am much more comfortable in vim
and command line stuff. I suppose I should use the IDE more.

you don't need to run the whole thing, of course; just structure your
code so you can work with individual modules, import *those* modules
into the command line interface, and call their contents from there.

cutting and pasting and rewriting code to be able to test it strikes me
as tedious and error prone compared to the alternative.

(and this has nothing to with IDE:s. I've worked full time with Python
for ages, and I still do 99.5% of my programming with an editor and a
command line)

</F>
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top