[IPython-user] Newbie - functions at prompt, functions in script
Fernando.Perez at colorado.edu
Thu May 19 13:29:48 CDT 2005
Gary Taylor wrote:
> 1. When I'm at the prompt and want to construct this simple
> "program", how do I do that when I need to hit enter after
> the line that reads print "First Line." ?
> def newLine():
> print "my function"
> print "First Line."
> print "Second Line."
The only difference between defining functions interactively and in a script,
is that when working interactively, a blank line tells the interpreter that
you're finished. So you can't have blank lines within a function definition.
In a file, there is no such restriction.
This is OK, since it's rare to edit large functions interactively. If you are
going to be editing anything that's more than a handful of lines, you should
work in an editor to be able to comfortably save and modify your work. You
can use ipython's 'run' command (type 'run?' for details) to run your file
each time you change it.
> 2. When I put this in a script, it only works if def
> newLine() is before newLine() . Does it have to be first
> or am I doing something incorrect?
Yes, you must first define the function before you use it.:
def foo(): pass
foo() # OK
bar() # NOT OK, bar is not known yet.
def bar(): pass
Hope this helps,
More information about the IPython-user