newbie questions

Michael Hudson mwh21 at cam.ac.uk
Fri Jun 11 13:28:45 EDT 1999


Jeff Norden <jeff at norden.math.tntech.edu> writes:

> I've been tinkering with python for a few days.  I have to admit that my
> first impression was that this language would turn me into a newt, but I got
> better :-)
> 
> A few questions:
> 
> 1) Is there no equivalent to perl's  "use strict" in which all variables
> have to be declared?  How about perl's "-w" flag, which at least gives a
> warning when a variable is only used once.  These features are nice for
> catching common mistakes, i.e., if you try to increment spam with
> 
>    span = spam + 1
> 
> python will just create a new span variable, and no error message.
> Or, consider the even more insidious error:   vikingl = viking1 + spam

Python has no declarations at all, ever (I think). There's some
kpylint package found off of www.chordate.com that will check for
identifiers that are only used once. It's not incredibly bright (it
doesn't really try to be).

> 2) Is the above the correct way to increment a variable? 

Yes.

> I can't find an
> equivalent for "++" or "+="

That would be because there isn't one. You could use my bytecodehacks
package to fake them, but don't ;-).

> 3) These are rather minor, but is there no way to "forward declare" function
> definitions? 

No.

> I'd rather put them at the end of the file instead of the
> beginning.  Python does parse the whole file before it starts execution,
> doesn't it?  

Yes.

> Also, is there a way to just check for syntax errors without
> execution, like perl's "-c" switch?

Well, you could try this:

def check_file(file):
    import parser
    try:
        parser.suite(open(file).read())
        print "no syntax errors"
    except parser.ParserError:
        print "syntax error"

this was thrown together in about a minute.
seems to work.

HTH
Michael




More information about the Python-list mailing list