Newbie's Questions

Alex Martelli aleaxit at yahoo.com
Mon Feb 19 07:26:31 EST 2001


"Thomas Guettler" <guettli at interface-business.de> wrote in message
news:mailman.982581805.9060.python-list at python.org...
> Yesterday I had time to start learning python. I like it, after a half

Bravo!-)

> hour I wasn't missing the "{..}" anymore. Here are some questions I
> have:
>
> Is there a way of checking the program when I start it.  So typos are
> detected emidiately, and not when I enter the function.

The syntax of the whole script IS checked when you start it,
since it's internally compiled then; this will catch many
typos -- e.g., if you write:
    fi x>0:
rather than
    if x>0:
a SyntaxError will be generated immediately -- but not, of
course, all, e.g., if you write:
    x=y=z
rather than
    x=y+z
(also a frequent occurrence, since + and = are shifted
version of each other on many keyboards), this will not be
detected until you run suitable tests, as both forms happen
to be syntactically correct.

There is no way to even check for typos _that will
generate exceptions when the function is run_, without
actually running the function; e.g.,
    a=b/c
where one meant to write
    a=b/d
and c happens to be 0, will be caught when the function
is run (the division-by-zero attempt will raise an
exception), but not before then.  And similarly if c
happens to refer to a string (you can't divide by a
string), or if it happens to be unbound at that point;
only actual execution, not compilation, will reveal it.


> Is there something like "use strict" in perl? I like to declare my
> variables before I use them. (Again, to recognize typos)

There are no declarations in Python (the 'global' statement
_might_ be thought of as an exception to this assertion...).
If you're using a variable that's not bound at all, at run
time, or that is bound to a string where a number is needed
at that point, etc, etc, you'll get runtime exceptions; no
way to have the compiler diagnose it beforehand.


> Is there a way of reading a single character from stdin without
> waiting for the return-key? (At best without curses)

On Windows, only, the msvcrt module offers functions getch
(to read a single keypress, without waiting for a return, and
without echoing) and getche (ditto, with echoing); but they
don't work on standard-input (i.e., they are immune to stdin
redirections), but, rather, on the actual keyboard.

On Unix, you can use the low-level module termios, or the
higher-level one tty that in turn uses termios, to set the
mode to 'raw' for file-descriptor 0 (standard input).


> Is there something like "exit" in C? Yes, I could just raise an
> exception, but I prefere a way without a stacktrace.

Module sys contains function exit, which in turn raises a
SystemExit exception.  Either calling sys.exit(), or raising
SystemExit directly, will NOT generate a stacktrace -- but
the exception can be caught, so that try/finally and
try/except semantics are preserved.  If you need a way
to exit in an emergency, *without* proper/normal actions
on termination (no flushing of buffers, no finally blocks,
etc, etc), you can do that with function _exit of module
os, but that's a pretty-rare need ("should normally only
be used in the child process after a fork()" is what the
docs say, but os.fork is only available on Unix platforms,
while os._exit is also available on Windows).


Alex






More information about the Python-list mailing list