473,574 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"no variable or argument declarations are necessary."

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?
--
http://www.jim.com
Oct 2 '05 #1
134 7808
D H
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?


It's a fundamental part of python, as well as many other scripting
languages.
If you're not comfortable with it, you might try a language that forces
you to declare every variable first like java or C++.
Otherwise, in python, I'd recommend using variable names that you can
easily spell. Also do plenty of testing of your code. It's never been
an issue for me, although it would be nicer if python were
case-insensitive, but that is never going to happen.
Oct 2 '05 #2
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 "<interacti ve 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*jvyyzptht na^pbz")
Oct 2 '05 #3
Wow, never even occured ot me someone would have a problem with this!

But, this might help:

http://www.logilab.org/projects/pylint

In more detail:
Surely that means that if I misspell a variable name, my program will
mysteriously fail to work with no error message.
No, the error message will be pretty clear actually :) You are
attempting to use a variable that doesn't exist! This would be the same
type of message you would get from a compiled language, just at a
different point in time (runtime vs. compile time).
If you don't declare variables, you can inadvertently re-use an
variable used in an enclosing context when you don't intend to,
Possible, though good design should always keep anysuch situation at
bay. Python is OO, hence scoping should rarely be a problem ... globals
are mostly evil, so the context at any given time should be the method,
you'd need a fairly big and complex method to start loosing track of
what you called what ... Also, a good naming convention should keep this
at bay.

Also, because things are interpreted, you don't (normally) need to put
extensive forthought into things as you do with compiled languages. You
can run things quickyl and easily on demand, a misnamed variable will be
clearly indicated and easily solved in a matter of minutes.

Using a smart IDE might also help prevent such problems before they occur?

Hope you enjoy python :)

J.F.

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?
--
http://www.jim.com

Oct 2 '05 #4
The easiest way to avoid this problem (besides watching for NameError
exceptions) is to use an editor that has automatic name completion.
Eric3 is a good example. So, even though in theory it could be an
issue, I rarely run into this in practice.

-Don

Oct 2 '05 #5
James A. Donald:
> Surely that means that if I misspell a variable name, my program will
> mysteriously fail to work with no error message.

On Sun, 02 Oct 2005 17:11:13 -0400, Jean-François Doyon No, the error message will be pretty clear actually :)


Now why, I wonder, does this loop never end :-)
egold = 0
while egold < 10:
ego1d = egold+1
--
http://www.jim.com
Oct 3 '05 #6
James A. Donald wrote:
On Sun, 02 Oct 2005 17:11:13 -0400, Jean-François Doyon
James A. Donald:
> Surely that means that if I misspell a variable name, my program will
> mysteriously fail to work with no error message.

No, the error message will be pretty clear actually :)

Now why, I wonder, does this loop never end :-)
egold = 0
while egold < 10:
ego1d = egold+1


I know (hope! :-) that's a tongue-in-cheek question, however the answer as
to why that's not a problem is more to do with development habits rather
than language enforcement. (yes with bad habits that can and will happen)

Much python development is test-driven. Either formally using testing
frameworks (I'm partial to unittest, but others like other ones), or
informally using a combination of iterative development and the
interactive shell. Or a mix of the two.

With a formal test framework you would have noticed the bug above
almost instantly - because your test would never finish (Which would
presumably count as a failure for the test that exercises that code).

Whilst that might seem odd, what you're actually doing with type
declarations is saying "if names other than these are used, a bug
exists" and "certain operations on these names are valid". (as well
as a bunch of stuff that may or may not relate to memory allocation
etc)

With test driven development you are specifically testing the functionality
you want to exist *does* exist. TDD also provides a few tricks that can
help you get around writers block, and also catch bugs like above easily and
more importantly early.

Bruce Eckel (author of a fair few interesting C++ & Java books :-) has a
couple of interesting essays on this topic which I think also take this
idea a lot further than is probably suitable for here:

* Strong Typing vs. Strong Testing:
http://www.mindview.net/WebLog/log-0025
* How to Argue about Typing
http://www.mindview.net/WebLog/log-0052

For what it's worth, if you've not come across test driven development
before then I'd highly recommend Kent Beck's "Test Driven Development: By
Example". You'll either love it or hate it. IMO, it's invaluable though!
I suppose though the difference between static types based testing and
test driven development is that static types only really help you find
bugs (in terms of aiding development), whereas TDD actually helps you
write your code. (Hopefully with less bugs!)

Best Regards,
Michael.

Oct 3 '05 #7
"Michael" <ms@cerenity.or g> wrote:
James A. Donald wrote:
On Sun, 02 Oct 2005 17:11:13 -0400, Jean-Francois Doyon
James A. Donald:
> Surely that means that if I misspell a variable name, my program will
> mysteriously fail to work with no error message.
No, the error message will be pretty clear actually :)

Now why, I wonder, does this loop never end :-)
egold = 0
while egold < 10:
ego1d = egold+1


I know (hope! :-) that's a tongue-in-cheek question, however the answer as
to why that's not a problem is more to do with development habits rather
than language enforcement. (yes with bad habits that can and will happen)

[snipped description of test-driven development culture]


As an aside, more to the point of the specific erroneous example is the lack of the standard python
idiom for iteration:

for egold in xrange(10):
pass

Learning and using standard idioms is an essential part of learning a language; python is no
exception to this.

George
Oct 3 '05 #8
Op 2005-10-03, George Sakkis schreef <gs*****@rutger s.edu>:
"Michael" <ms@cerenity.or g> wrote:
James A. Donald wrote:
> On Sun, 02 Oct 2005 17:11:13 -0400, Jean-Francois Doyon
> James A. Donald:
>> > Surely that means that if I misspell a variable name, my program will
>> > mysteriously fail to work with no error message.
>> No, the error message will be pretty clear actually :)
> Now why, I wonder, does this loop never end :-)
> egold = 0
> while egold < 10:
> ego1d = egold+1


I know (hope! :-) that's a tongue-in-cheek question, however the answer as
to why that's not a problem is more to do with development habits rather
than language enforcement. (yes with bad habits that can and will happen)

[snipped description of test-driven development culture]


As an aside, more to the point of the specific erroneous example is the lack of the standard python
idiom for iteration:

for egold in xrange(10):
pass

Learning and using standard idioms is an essential part of learning a language; python is no
exception to this.


Well I'm a bit getting sick of those references to standard idioms.
There are moments those standard idioms don't work, while the
gist of the OP's remark still stands like:

egold = 0:
while egold < 10:
if test():
ego1d = egold + 1

--
Antoon Pardon
Oct 3 '05 #9
Antoon Pardon wrote:
Well I'm a bit getting sick of those references to standard idioms.
There are moments those standard idioms don't work, while the
gist of the OP's remark still stands like:

egold = 0:
while egold < 10:
if test():
ego1d = egold + 1


Oh come on. That is a completely contrived example, and besides you can
still rewrite it easily using the 'standard idiom' at which point it
becomes rather clearer that it is in danger of being an infinite loop even
without assigning to the wrong variable.

for egold in range(10):
while test():
pass

I find it very hard to believe that anyone would actually mistype ego1d
while intending to type egold (1 and l aren't exactly close on the
keyboard), and if they typed ego1d thinking that was the name of the loop
variable they would type it in both cases (or use 'ego1d += 1') which would
throw an exception.

The only remaining concern is the case where both ego1d and egold are
existing variables, or more realistically you increment the wrong existing
counter (j instead of i), and your statically typed language isn't going to
catch that either.

I'm trying to think back through code I've written over the past few years,
and I can remember cases where I've ended up with accidental infinite loops
in languages which force me to write loops with explicit incremements, but
I really can't remember that happening in a Python program.

Having just grepped over a pile of Python code, I'm actually suprised to
see how often I use 'while' outside generators even in cases where a 'for'
loop would be sensible. In particular I have a lot of loops of the form:

while node:
... do something with node ...
node = node.someAttrib ute

where someAttribute is parentNode or nextSibling or something. These would,
of course be better written as for loops with appropriate iterators. e.g.

for node in node.iterAncest ors():
... do something with node ...
Oct 3 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6260
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments passed to the function are strings or must be (automaticly converted to a string e.g. the number 10 should become the string "10". My problem is that I...
7
8313
by: | last post by:
How to call a function with variable argument list from another function again with variable argument list? Example : double average ( int num, ... ); double AFunct1 ( int num, ... ); double AFunct2 ( int num, ... ); double average ( int num, ... )
3
4412
by: zoe | last post by:
I am trying to master the variable argument list! C99 tells me: 7.15.1.4 The va_start macro: 1 #include <stdarg.h> void va_start(va_list ap, parmN); 4 The parameter parmN is the identifier of the rightmost parameter in
5
1685
by: Andrej Prsa | last post by:
Hi! Why do I get a warning about incompatible pointer type if I try to assign a pointer to the function with variable argument number: int func (int argc, ...) , but everything is ok when I'm using a simple function like: int func (int argc, char *whatever)
1
3134
by: Ben Kial | last post by:
I'd like to write a wrapper function "mysprintf(char *buffer, char *format, ....)" which calls sprintf(). My question is how can I pass variable argument in mysprintf() to sprintf(). Thanks in advance... Ben
1
2196
by: S?ren Gammelmark | last post by:
Hi I have been searching the web and comp.lang.c of a method of using variable-argument function pointers and the like. And some of the questions arising in this post are answered partly in these posts, but I ask mainly for a way to found a way to solve this problem. I'm a hobbyist so there might be an alltogether better solution to the...
2
2905
by: William Ahern | last post by:
So, I recently learned that Solaris doesn't, or doesn't seem, to provide err.h. On some platforms err.h provides simple wrappers for error printing to stderr. One of which, err(), has this signature: void err(int exit_code, const char *fmt, ...); It will print fmt + ... to stderr, tacking on a colon, the result of strerror(errno), and a...
5
2993
by: shaanxxx | last post by:
i have statements printf("%f",(double)1); /* works fine although i have sent %f for double */ printf("%c",(int)64); /* works fine although i have sent %c for int */ Need your comment on this. As my understanding, when we pass something on variable argument, its type will be promoted . char is promoted to Int . float is promoted to
19
2114
by: Spiros Bousbouras | last post by:
Every time I've seen an example of a variable argument list function its functionality was to print formatted output. Does anyone have examples where the function is not some variation of printf ?
0
7807
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8058
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7820
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5301
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.