Re: "no variable or argument declarations are necessary."



James A. Donald wrote:
I am contemplating getting into Python, which is used by engineers I
admire - google and Bram Cohen, but was horrified to read

"no variable or argument declarations are necessary."

Surely that means that if I misspell a variable name, my program will
mysteriously fail to work with no error message.

If you don't declare variables, you can inadvertently re-use an
variable used in an enclosing context when you don't intend to, or
inadvertently reference a new variable (a typo) when you intended to
reference an existing variable.

What can one do to swiftly detect this type of bug?

A variable has to be assigned to before it is used, otherwise a NameError exception is thrown..


>>> a + 1
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
NameError: name 'a' is not defined
>>> a = 1
>>> a + 1
2

Typos in variable names are easily discovered unless the typo happens to exist in the current context.

Will McGugan
--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in "jvyy*jvyyzpthtna^pbz")
.




Relevant Pages

  • Re: A Question From An Outsider
    ... "rank amateurs" violated your privacy by going to that page of your public web site without your prior, express, written consent to do so. ... That's a clear typo on my part that crept in during the transfer of the text ... read, in fact, "Did he dare risk a cell phone call?" ... I intend letting it ...
    (rec.arts.mystery)
  • Re: An altercation with Harlan Ellison
    ... I intend to steal it. ... Typo, but as you note, perhaps ironically more on-target than what was ... >> seem to be the sort of stupid questions that fans ask over and over ... I told him that I was calling from work, and he said "Oh, ...
    (rec.arts.sf.written)
  • Re: This one needs a (booby) prize named after it
    ... Bill, I am so very sorry that I dared to riff on your typo. ... intend to insult or harm you in any way. ...
    (rec.bicycles.tech)
  • Re: On-topicness
    ... Remove the 'v' in void and I'd still call it a C program. ... typo. ... You run into the same problem of defining what ... is a C program because I intend to see how well it fits the ISO C ...
    (comp.lang.c)