Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I catch misnamed variables?

17 views
Skip to first unread message

John Gordon

unread,
Feb 10, 2012, 4:06:58 PM2/10/12
to
Recently I was been bitten by some stupid errors in my code, and I'm
wondering if there's a simple way to catch them.

One error was of the form:

my_object.some_function()

.. when I hadn't declared an object named "my_object".

The other error was similar:

x = my_module.CONSTANT

.. when I hadn't imported my_module.

Of course both of these errors were deep inside a long-running function
call, so it took a while for them to crop up.

Is there an automated way to catch errors like these? I'm using the
compileall module to build my program and it does catch some errors
such as incorrect indentation, but not errors like the above.

--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Arnaud Delobelle

unread,
Feb 10, 2012, 4:17:02 PM2/10/12
to John Gordon, pytho...@python.org
On 10 February 2012 21:06, John Gordon <gor...@panix.com> wrote:
> Recently I was been bitten by some stupid errors in my code, and I'm
> wondering if there's a simple way to catch them.
>
> One error was of the form:
>
>  my_object.some_function()
>
> .. when I hadn't declared an object named "my_object".
>
> The other error was similar:
>
>  x = my_module.CONSTANT
>
> .. when I hadn't imported my_module.
>
> Of course both of these errors were deep inside a long-running function
> call, so it took a while for them to crop up.
>
> Is there an automated way to catch errors like these?  I'm using the
> compileall module to build my program and it does catch some errors
> such as incorrect indentation, but not errors like the above.

There's pychecker and pylint

--
Arnaud

Kev Dwyer

unread,
Feb 10, 2012, 4:22:32 PM2/10/12
to pytho...@python.org
John Gordon wrote:

> Recently I was been bitten by some stupid errors in my code, and I'm
> wondering if there's a simple way to catch them.
>
<snip>


Pyflakes is another static checker that can catch these sorts of errors.

Cheers,

Kev


Ben Finney

unread,
Feb 10, 2012, 4:26:01 PM2/10/12
to
John Gordon <gor...@panix.com> writes:

> Is there an automated way to catch errors like these?

Use a static code checker, such as ‘pyflakes’ (simple but limited) or
‘pylint’ (complex but highly configurable) to catch these and many other
problems in Python code.

--
\ “It's a terrible paradox that most charities are driven by |
`\ religious belief.… if you think altruism without Jesus is not |
_o__) altruism, then you're a dick.” —Tim Minchin, 2010-11-28 |
Ben Finney

Ethan Furman

unread,
Feb 10, 2012, 4:56:38 PM2/10/12
to Ben Finney, pytho...@python.org
Ben Finney wrote (from signature):
> “It's a terrible paradox that most charities are driven by religious
> belief. . . . if you think altruism without Jesus is not altruism,
> then you're a dick.” —Tim Minchin, 2010-11-28

1) Why is it paradoxical? If anything it's a sad commentary on those
who don't ascribe to a religion, as it would appear that they care less
for their society.

2) altruism: unselfish regard for or devotion to the welfare of
others... no mention of religion of any kind, or Jesus in particular.

Altruistic-yet-paradoxically-religious-ly yours,
~Ethan~

Ben Finney

unread,
Feb 10, 2012, 5:45:28 PM2/10/12
to

Thanks for responding. Rather than take this discussion too far where
it's quite off-topic, I'll respond briefly and ask for a change of forum
if we want to continue.


Ethan Furman <et...@stoneleaf.us> writes:

> Ben Finney wrote (from signature):
> > “It's a terrible paradox that most charities are driven by religious
> > belief. . . . if you think altruism without Jesus is not altruism,
> > then you're a dick.” —Tim Minchin, 2010-11-28

The quote is from an interview with Tim Minchin
<URL:http://www.guardian.co.uk/stage/2010/nov/28/tim-minchin-comedian>.

> 1) Why is it paradoxical? If anything it's a sad commentary on those
> who don't ascribe to a religion, as it would appear that they care
> less for their society.

It's an outcome of history that religious institutions have historically
been well-situated to be the facilitators of charitable work (and much
other work) simply because they have been ubiquitous in most societies.
The paradox is that they spend much of their resources away from the
worldly, i.e. secular, work of charity.

But charitable work is not dependent on religious belief, and indeed in
recent decades there are now a great many wholly secular charities (e.g.
International Red Cross and Oxfam) which do not divert their resources
from addressing the real world.

> 2) altruism: unselfish regard for or devotion to the welfare of
> others... no mention of religion of any kind, or Jesus in particular.

Yes, that's the point. Altruism is a human activity independent of
religious belief, yet the default assumption of too many is that they
are somehow necessarily connected.

> Altruistic-yet-paradoxically-religious-ly yours,

As you rightly point out, this discussion is off-topic here. So while
I'm open to discussion on this topic, we should move it to some other
forum.

--
\ “Most people, I think, don't even know what a rootkit is, so |
`\ why should they care about it?” —Thomas Hesse, Sony BMG, 2006 |
_o__) |
Ben Finney

Christian Heimes

unread,
Feb 10, 2012, 6:21:11 PM2/10/12
to pytho...@python.org
Am 10.02.2012 22:06, schrieb John Gordon:
> Is there an automated way to catch errors like these? I'm using the
> compileall module to build my program and it does catch some errors
> such as incorrect indentation, but not errors like the above.

Write unit tests and use coverage to aim for 100% code and branch coverage.

If you want to write high quality code and avoid problems like misnamed
variables then you have to write unit tests and functional tests for
your program. I'm well aware that it's hard and requires time. But in
the long run it will *save* lots of time.

0 new messages