I just started messing with Python, and was having a great time with entering Tkinter GUI commands into the Python shell (ActivePython 2.5).
Its fun entering commands one at a time and seeing components appear real-time (instead of normal process of having to compile the whole thing first).

Anyway, I was wondering if there was a way to paste multiple lines of source code (at once).

Also: Is there a function that causes a string to be interpreted as a command?

Recommended Answers

All 6 Replies

I just started messing with Python, and was having a great time with entering Tkinter GUI commands into the Python shell (ActivePython 2.5).
Its fun entering commands one at a time and seeing components appear real-time (instead of normal process of having to compile the whole thing first).

Anyway, I was wondering if there was a way to paste multiple lines of source code (at once).

Also: Is there a function that causes a string to be interpreted as a command?

I suggest the standard IDLE instead. I found ActivePython lacking in features (dedent region, indent region, comment region, uncomment region) yet containing no new (useful) ones.

As for pasting multiple lines: well, you can paste multiple lines, but it only seems to evaluate the first one.

What you can instead do is nest those multiple lines in a function, and then paste it.

def test():
  line1 = 'bah'
  line2 = 'foo'
  print line2 + line1

now type test() for the result 'foobah'

whereas

line1 = 'bah'
line2 = 'foo'
print line2 + line1

would only evaluate line1 = 'bah'

This is only marginally easier than creating an actual script, since you get to avoid creating a file just for a test.

Re using strings as commands... what's the context?

Oh, and I highly recommend wxPython over Tkinter. Much more powerful, intuitive and easy to use, IMHO. Also newer: http://www.wxpython.org/quotes.php

Note the words by Eric S. Raymond and Guido van Rossum (Python creator).

The Python function eval() will take a string and try to evaluate/interpret it.

BTW, the PyScripter IDE allows you to interpret your source code without having to create an official file. I hate the Python shell, it's okay for one liners only!

If I were stranded on an island and were only allowed one Python GU toolkit, I would take wxPython. Actually, Fuse left a real nice introduction to wxPython programming here:
http://www.daniweb.com/forums/post621707-8.html

I tried eval('a=1') but it didn't work. I looked up the syntax for eval and discovered that exec was the function I was looking for.

>BTW, the PyScripter IDE allows you to interpret your source code without having to create an official file. I hate the Python shell, it's okay for one liners only!

Thanks!

Is there a way to run a segment of source code, then open a shell that has access to (and can change) the same variables and functions that were defined in the script?

>As for pasting multiple lines: well, you can paste multiple lines, but it only seems to evaluate the first one.

>What you can instead do is nest those multiple lines in a function, and then paste it.

>

def test():
  line1 = 'bah'
  line2 = 'foo'
  print line2 + line1

Thanks for the tip.

There is a little IDE called PyPE that allows you to run selected code in the shell. It has its own more advanced shell window.

There is a little IDE called PyPE that allows you to run selected code in the shell. It has its own more advanced shell window.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.