How do you program in Python?

A

anthonyberet

My question isn't as all-encompassing as the subject would suggest...

I am almost a Python newbie, but I have discovered that I don't get
along with IDLE, as i can't work out how to run and rerun a routine
without undue messing about.

What I would really like is something like an old-style BASIC
interpreter, in which I could list, modify and test-run sections of
code, to see the effects of tweaks, without having to save it each time,
or re-typing it over and over (I haven't even worked out how to cut and
paste effectively in the IDLE environment).

I see lots of alternate IDEs etc, but which would allow me the simple
interface that I have described? - I really don't know about IDEs in
general, and I suspect I would be out of my depth with one of those.

Thanks, and feel free to mock ;)
 
D

Diez B. Roggisch

anthonyberet said:
My question isn't as all-encompassing as the subject would suggest...

I am almost a Python newbie, but I have discovered that I don't get
along with IDLE, as i can't work out how to run and rerun a routine
without undue messing about.

What I would really like is something like an old-style BASIC
interpreter, in which I could list, modify and test-run sections of
code, to see the effects of tweaks, without having to save it each time,
or re-typing it over and over (I haven't even worked out how to cut and
paste effectively in the IDLE environment).

I see lots of alternate IDEs etc, but which would allow me the simple
interface that I have described? - I really don't know about IDEs in
general, and I suspect I would be out of my depth with one of those.

The property of basic to have explicit linenumbering is the reason
you've been able to work in that style of programming, as you could
simply overwrite parts of the program. But that's close to undoable in
all other languages.

Python has the interactive REPL that you can use to toy around
interactively and is very helpful to me - especially when enriched with
rlcompleter2. - You can even replace functions or classes while working
and toying around. But the amount of reasonable editing work that can be
done in the interpreter is pretty limited. You _need_ a decent editor
for that.

So what I usually do is to create a test.py that contains the code I
want to tinker with, and simply run that in a shell - cygwin on windows,
but usually I work in *NIX-style environments, so it's even easier. As
there is no compilation or whatsoever required, that works pretty well
for me.

Diez
 
P

Peter Hansen

anthonyberet said:
What I would really like is something like an old-style BASIC
interpreter, in which I could list, modify and test-run sections of
code, to see the effects of tweaks, without having to save it each time,
or re-typing it over and over (I haven't even worked out how to cut and
paste effectively in the IDLE environment).

I do all my work using Scite (a nice free editor that was built to
demonstrate the Scintilla plugin that can also be used in Python
programs through things like the StructuredTextControl in wxPython),
with the auto-save-on-loss-of-focus feature enabled, and a command
prompt open in another window.

I edit in the Scite window, hit Alt-Tab (under Windows XP) to change
focus to the cmd console (and instantly all my modified files are
saved), press the Cursor Up key to retrieve the previous command (which
is generally the name of my script, or a command like "python
myscript.py"), and hit Enter to execute it.

So, any time I need to test the changes, I hit four keys (which at this
point is understandably more like a "chord" that I hit without direct
awareness of it) and I'm done. Sounds pretty close to old-style BASIC
and since I've come that route too (in the distant past), this may not
be a coincidence.

-Peter
 
M

mensanator

anthonyberet said:
My question isn't as all-encompassing as the subject would suggest...

I am almost a Python newbie, but I have discovered that I don't get
along with IDLE, as i can't work out how to run and rerun a routine
without undue messing about.

What I would really like is something like an old-style BASIC
interpreter, in which I could list, modify and test-run sections of
code, to see the effects of tweaks, without having to save it each time,
or re-typing it over and over (I haven't even worked out how to cut and
paste effectively in the IDLE environment).

I think you're missing something about IDLE if you are re-typing.

For example:

IDLE 1.1a3 print i,

When you type the above, you get

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Now let's say you want to re-run that with 10 instead of 25.
You don't have to re-type it. Click to place your cursor anywhere
print i,

The cursor will be at the very end of the block so you can re-run
it by hitting <enter> twice. But you'll get the same output.
Instead of hitting <enter> twice, use your cursor keys (or mouse)
to move to the 25 in the range function and edit it to 10. Once
print i,


0 1 2 3 4 5 6 7 8 9

It sounds worse than it is, just takes a little getting used to.

Once you're happy with that code fragment, edit it again and add
a definition at the start.

print i,

[2] place cursor in front of the 'for' and add a function name
and change the 'for' to use the parameter instead of a constant
print i,

for i in range(n):
print i,

[4] move cursor to the end of the block (after the comma)
and hit <enter> twice

Nothing happens. You have defined a function, but did not execute
it. To execute it, type its name with some value of n.
0 1 2 3 4 5 6 7

Now you can try it out with different values of n without having
to edit the whole block. Now if you want to change the function
definition, say we want each number output on a seperate line,
do the same thing we did before. Click anywhere in the block and
hit <enter>. The entire block is re-typed for you. Make your change
for i in range(n):
print i

Again, the function is not executed. And you can always simply
 
M

Michael Linnemann

Am Sun, 03 Jul 2005 13:40:04 -0400 schrieb Peter Hansen:
I do all my work using Scite (a nice free editor that was built to
demonstrate the Scintilla plugin that can also be used in Python
programs through things like the StructuredTextControl in wxPython),
with the auto-save-on-loss-of-focus feature enabled, and a command
prompt open in another window.
I edit in the Scite window, hit Alt-Tab (under Windows XP) to change
focus to the cmd console (and instantly all my modified files are
saved), press the Cursor Up key to retrieve the previous command (which
is generally the name of my script, or a command like "python
myscript.py"), and hit Enter to execute it.

I do the same thing without a command prompt, just pressing F5. SciTE
sure is a good editor :)

Regards
Michael
 
J

James

Peter said:
I do all my work using Scite (a nice free editor that was built to
demonstrate the Scintilla plugin that can also be used in Python
programs through things like the StructuredTextControl in wxPython),
with the auto-save-on-loss-of-focus feature enabled, and a command
prompt open in another window.

I edit in the Scite window, hit Alt-Tab (under Windows XP) to change
focus to the cmd console (and instantly all my modified files are
saved), press the Cursor Up key to retrieve the previous command (which
is generally the name of my script, or a command like "python
myscript.py"), and hit Enter to execute it.

So, any time I need to test the changes, I hit four keys (which at this
point is understandably more like a "chord" that I hit without direct
awareness of it) and I'm done. Sounds pretty close to old-style BASIC
and since I've come that route too (in the distant past), this may not
be a coincidence.

Just curious. Why do you Alt-Tab to a console when you can just hit F5
in SciTE? That's just 1 key instead of 4. And yes, SciTE can autosave
here too.
 
M

Mike Meyer

anthonyberet said:
My question isn't as all-encompassing as the subject would suggest...

I am almost a Python newbie, but I have discovered that I don't get
along with IDLE, as i can't work out how to run and rerun a routine
without undue messing about.

What I would really like is something like an old-style BASIC
interpreter, in which I could list, modify and test-run sections of
code, to see the effects of tweaks, without having to save it each
time, or re-typing it over and over (I haven't even worked out how to
cut and paste effectively in the IDLE environment).

I see lots of alternate IDEs etc, but which would allow me the simple
interface that I have described? - I really don't know about IDEs in
general, and I suspect I would be out of my depth with one of those.

Thanks, and feel free to mock ;)

Try an emacs (Xemacs is my preference). That should come with a python
editing mode, which includes the ability to send code to a Python
interpreter running in interactive mode in another window. So what you
do is edit the code in a file on disk, and then ship it all to the
interpreter (with C-c C-c). You can then use the objects defined in
the file at the interperter prompt. After you identify a bug, you edit
the function/method in question, and ship the function/class to the
interpreter with M-C-x. You can then start playing with the new
version of the function/class to check it.

Note that this behavior isn't specific to Python. It's pretty standard
emacs support for all languages that have a REPL loop. The command
keys are even the same if you change languages.

Be warned - changing a class doesn't change the code of any objects
that were defined using the old class. So you get this sequence:

x = MyClass()
x.Test() # reveal bug in MyClass
# edit MyClass.py, fix the bug, and M-C-x the class
x.Test() # The bug is still there!
x = MyClass()
x.Test() # Now it's gone.

<mike
 
P

Peter Hansen

James said:
Just curious. Why do you Alt-Tab to a console when you can just hit F5
in SciTE? That's just 1 key instead of 4. And yes, SciTE can autosave
here too.

Probably a variety of reasons. You might say ignorance is one of them
(since I wasn't consciously aware of that feature), but it's really that
I deliberately choose not to use and learn about such features of
editors because I absolutely _hate_ getting tied to such things and
finding myself stuck in other editors or on other machines and having my
productivity drop to zero because a simple feature is missing.

This is the same reason I no longer use any IDE, nor have any interest
whatsoever in them other than out of simple curiosity. I used to learn
all kinds of details about things like VisualCafe, CodeWright, IDLE, Boa
Constrictor, Borland C++, a dozen different Microsoft products... no
longer! If I didn't have Scite, I'd be able to use DOS Edit almost as
effectively, and by observation I've concluded that using such primitive
tools I'm often more effective than others around me who are "stuck"
using the latest featureful tools...

Also, I lied when I said cursor up retrieved the name of the script.
Since I develop using Test-Driven Development, and use a simple suite of
utilities to manage the test files and such, I'm more often executing
something like "story015 TestCase.test03", or perhaps just "u" (which
runs all unit tests in the current folder), rather than the name of the
script which I happen to be editing.

Judging by how F5 appears to work, it wouldn't save me keystrokes
either: I'd most likely be in some *other* file making changes (i.e. one
of the modules under test) but would need to hit Alt-9 (or a bunch of
Ctrl-Tabs) to switch to wherever story015.py was sitting. The file I
want to run won't always be the one under my cursor, nor perhaps even
loaded.

After 25 years doing this, I've become something of a Luddite as far as
fancy IDEs and non-standard features go... and a huge believer in strict
decoupling between my tools, to the point of ignoring things that bundle
them together in ways that are, in my opinion, too tight. Sorry! :)

-Peter
 
D

dirk dierickx

I see lots of alternate IDEs etc, but which would allow me the simple
interface that I have described? - I really don't know about IDEs in
general, and I suspect I would be out of my depth with one of those.

Eclipse and pydev are good. for testing out small routines you can use
ipython, which is great.
 
T

Terry Hancock

What I would really like is something like an old-style BASIC
interpreter, in which I could list, modify and test-run sections of
code, to see the effects of tweaks, without having to save it each time,
or re-typing it over and over (I haven't even worked out how to cut and
paste effectively in the IDLE environment).

How old style? Basic has changed a lot over the years. ;-)

But seriously, I think I know what you mean. I'm going to suggest that
you try going very "low tech" with this and don't use an IDE at all. I'm
sure you have some form of window environment (X, Windows, or Mac)
and can open a terminal and an editor of your choice on screen (even if
it's "Notepad").

Start the Python interpreter up in the terminal by just typing "python".
In the interpreter you can try simple one or two liners. You can even
define a short function and try it out.

But you'll get annoyed by having to "up-arrow" and retrieve lines one-
by-one to repeat the function after you've made a mistake, so that'll
get old for anything more than 5 lines long.

So, use your editor. Type up a short program that you want to test
in the editor, then copy and paste it into the Python interpreter. This
way, when you (inevitably) find mistakes, you can just tweak them in
the editor and then paste them again.

You can, of course, paste statements from the interpreter back into
your editor, but you'll have to avoid the ">>>" prompts (or delete them),
so it's usually better to write in the editor and paste into the interpreter.

And when you're done, you can just save the result to file so you
can get it back later.

My point is, you don't have to use a fancy all-in-one application to
do the job, nor must you run your programs from within your editor.
It's not so hard to just use tools you already understand.

Personally, I did things like this for years, and still do from time to time.
I've hardly ever used an IDE, and I'm still not totally convinced they
are worth the effort involved in learning them. I used to hop from computer
to computer a lot, and if I tried to learn my way around separate IDEs
on Macs, Windows, DOS, Solaris, and Debian Linux, I would've spent
a lot of time twiddling with the tools instead of just writing code.

Lately, I've settled into using Vim, which is nice editor (especially if you
once wrote a several thousand line program in Vi with nothing but a
fold-up quick reference for a manual ;-) ). There may well be friendlier
ones, but I know this one fairly well, and it has a pretty large community
of developers who are constantly tweaking it to make it a better editor
for programming. I only just recently started seriously customizing it
to work well with Python, though.

You can always go shopping for an IDE to buy into heavily *after*
you've learned the language. Meanwhile, don't let trifles get in the
way -- just use your window system as your IDE. That way, you
can pick the editor to suit you. And the Python interpreter is great,
it will give you no trouble and quick answers.
 
B

bruno modulix

anthonyberet said:
My question isn't as all-encompassing as the subject would suggest...

I am almost a Python newbie, but I have discovered that I don't get
along with IDLE, as i can't work out how to run and rerun a routine
without undue messing about.

What I would really like is something like an old-style BASIC
interpreter, in which I could list, modify and test-run sections of
code, to see the effects of tweaks, without having to save it each time,
or re-typing it over and over (snip)
I see lots of alternate IDEs etc, but which would allow me the simple
interface that I have described?

Try Emacs + python-mode. Emacs surely has a lot of warts, but I'm still
looking for a better and more versatile code editor/IDE - specially when
it comes to languages with REPL (-> Read-Eval-Print Loop).
 
R

Roy Smith

bruno modulix said:
Try Emacs + python-mode. Emacs surely has a lot of warts, but I'm still
looking for a better and more versatile code editor/IDE - specially when
it comes to languages with REPL (-> Read-Eval-Print Loop).

When you build Python, make sure you build it with Gnu readline support.
Then you can just fire up an interpreter, and use emacs (or, I suppose, vi)
editing commands to scroll back th
 
R

Roy Smith

bruno modulix said:
Try Emacs + python-mode. Emacs surely has a lot of warts, but I'm still
looking for a better and more versatile code editor/IDE - specially when
it comes to languages with REPL (-> Read-Eval-Print Loop).

When you build Python, make sure you build it with Gnu readline support.
Then you can just fire up an interpreter, and use emacs (or, I suppose, vi)
editing commands to scroll back through (and change) your input history.
It's not as good as a real IDE, but it's still very handy for quick
explorations.

The next step up would be to run a real emacs, do M-X shell, then fire up a
Python interpreter inside that.

Or, go into split screen mode in emacs, editing your python source file in
one window and running a shell in the other. Edit some code in the source
window, and it takes about 6 keystrokes to save it, flip to the other
window, and re-run the file (you can get it down to a single keystroke by
defining a simple macro and binding it to a function key).
 
B

bruno modulix

Roy said:
When you build Python, make sure you build it with Gnu readline support.
Then you can just fire up an interpreter, and use emacs (or, I suppose, vi)
editing commands to scroll back through (and change) your input history.
It's not as good as a real IDE, but it's still very handy for quick
explorations.

The next step up would be to run a real emacs, do M-X shell, then fire up a
Python interpreter inside that.

Or, go into split screen mode in emacs, editing your python source file in
one window and running a shell in the other. Edit some code in the source
window, and it takes about 6 keystrokes to save it, flip to the other
window, and re-run the file (you can get it down to a single keystroke by
defining a simple macro and binding it to a function key).

Err... actually, using python-mode is *way* more simple:

[ctrl+c ! to launch the REPL (this splits the frame) if it's not already
running]
[ctrl+x o to go back to the source code buffer]

then

ctrl+c ctrl+c to execute the whole buffer in the REPL
or
select a region then ctrl+c ctrl+l to execute the selected region in the
REPL

Of course, the REPL now reflect any changes made into the source code
buffer (even if changes have not been saved) !-)
 
A

Aahz

After 25 years doing this, I've become something of a Luddite as far as
fancy IDEs and non-standard features go... and a huge believer in strict
decoupling between my tools, to the point of ignoring things that bundle
them together in ways that are, in my opinion, too tight. Sorry! :)

+1 QOTW
 
C

Chris Smith

My question isn't as all-encompassing as the subject would
bruno> Try Emacs + python-mode. Emacs surely has a lot of warts,
bruno> but I'm still looking for a better and more versatile code
bruno> editor/IDE - specially when it comes to languages with REPL
bruno> (-> Read-Eval-Print Loop).

Emacs is an excellent long-term time investment.
See http://www.emacswiki.org for good justice.
R,
C
 
G

gene tani

+1 on investing in emacs. There's a shiny new release of the Oreilly,
which is (a really large book) that a lot of people teach themself out
of:

http://www.oreilly.com/catalog/gnu3/

But i would also recommend komodo from activestate's IDE, easy and
powerful, once you understand what all's in the menu- & toolbars:

http://www.activestate.com/Products/Komodo/?_x=1

(Maybe look at WingIDE, too, i've heard it's good)

And the python quick refs:

http://rgruet.free.fr/PQR24/PQR2.4.html
http://www.onlamp.com/pub/a/python/excerpt/PythonPocketRef/index.html
 
H

Harald Armin Massa

Peter,
I do all my work using Scite
Me too!
So, any time I need to test the changes, I hit four keys (which at this
point is understandably more like a "chord" that I hit without direct
awareness of it) and I'm done. Sounds pretty close to old-style BASIC
and since I've come that route too (in the distant past), this may not
be a coincidence.

in addition I have set up scite and paths so that "F5", the scite run
command, invokes python <myskript.py>, with output given in the scite
output area (and saving before)
in addition, ctrl+1 does a compile (checking for syntax errors)

and exceptions are printed out in the scite output, colourcoded and
with double-click on them scite opens the appropriate script at the
offending position.

VERY quick.

Harald
 
T

Tom Anderson


+1 insight of the century. This is the heart of the unix way - lots of
simple little programs that do exactly one thing well, and can be composed
through simple, clean interfaces. For actually getting things done, a
toolkit beats a swiss army knife.

tom
 
P

Peter Hansen

Tom said:
+1 insight of the century. This is the heart of the unix way - lots of
simple little programs that do exactly one thing well, and can be
composed through simple, clean interfaces. For actually getting things
done, a toolkit beats a swiss army knife.

Perhaps, but I'm puzzled how that explanation would apply to emacs and
those who use it as a swiss army knife, doing everything from editing to
email to laundry in the same editor...

(Note: this isn't a flame about emacs, nor vi for that matter, just a
discussion about the apparent conflict in the two philosophies embodied
by the "simple little programs" and the "emacs" approaches.)

-Peter
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top