Spelling mistakes!

K

KraftDiner

I've spent hours trying to find a bug that was a simple spelling
mistake.

in an init method I declare a variable self.someLongName

later in a different method of the class I use
self.sumLongName
Now I really meant self.someLongName.
In fact I don't want a variable called sumLongName.
Frankly how are you ever to know if this type of error is occuring?
 
H

Heiko Wundram

KraftDiner said:
Frankly how are you ever to know if this type of error is occuring?

By the traceback:

modelnine@phoenix ~ $ python
Python 2.4.2 (#1, Dec 22 2005, 17:27:39)
[GCC 4.0.2 (Gentoo 4.0.2-r2, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information..... def __init__(self):
.... self.somename = "hello"
.... def somemethod(self):
.... print self.sumname
....Traceback (most recent call last):
File "<stdin>", line 1, in ?

AttributeError describes just this. And please don't start the war on how
much better compiled languages are because they catch this kind of error at
compile time. They simply are not.

--- Heiko.
 
F

Fuzzyman

VBScript allows you to specify that variable names be declared. I used
to think this was good - until I realised that Python allows you to
dynamically assign attributes in namespaces using all sorts of tricks.

(Setattr, using __dict__ etc).

It's just not possible with Python. Rarely happens to me in practise
and is easy to fix.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
K

KraftDiner

try this:

class x(object):
def __init__(self):
self.someName = "hello"
def someMethod(self):
self.sumName = "bye"

find that bug.
 
?

=?iso-8859-1?B?QW5kcuk=?=

KraftDiner said:
try this:

class x(object):
def __init__(self):
self.someName = "hello"
def someMethod(self):
self.sumName = "bye"

find that bug.

Write a test for each method before writing the method.

Write the code once; read it critically (at least) twice.

If you find that too difficult, use a different programming language
(one in which you have to declare every variable) and become less
productive.

A.
 
X

Xavier Morel

KraftDiner said:
I've spent hours trying to find a bug that was a simple spelling
mistake.

in an init method I declare a variable self.someLongName

later in a different method of the class I use
self.sumLongName
Now I really meant self.someLongName.
In fact I don't want a variable called sumLongName.
Frankly how are you ever to know if this type of error is occuring?

PyChecker and PyLint sound like the perfect remedy to this issue (I know
one of them is able to warn you if you *create* an attribute outside of
__init__, maybe both are but at least one of them is)

I just run them when I save my files, they don't take long and even
though the default configuration is *extremely* annoying (especially
PyLint's, it generates heaps of warnings) once configured to one's need,
they're extremely valuable for both personal and team development.
 
P

Peter Hansen

KraftDiner said:
try this:

class x(object):
def __init__(self):
self.someName = "hello"
def someMethod(self):
self.sumName = "bye"

find that bug.

You forgot to include unit tests for someMethod(). Those would have
caught the bug.

The reality is that if you aren't doing unit testing, you aren't
catching other bugs either, bugs which aren't caught by the compile-time
tests in other languages and which aren't caught by PyChecker or PyLint
either. Bugs which are probably much more common than typos like the
above, too.

Still, if you are really bothered by these things frequently, I believe
recipes have been posted in the past which *do* allow you to avoid many
such problems, by declaring the acceptable attribute names somewhere and
using one of Python's many hooks to intercept and check attribute
setting. Check the archives or the CookBook.

-Peter
 
S

skip

Aside from the other responses (unittests, pychecker/pylint), you might also
consider using __slots__ for new-style classes:

class x(object):
__slots__ = ('someName',)
def __init__(self):
self.someName = "hello"
def someMethod(self):
self.sumName = "bye"

my_x = x()
my_x.someMethod()

When run you will get this output:

Traceback (most recent call last):
File "slot.py", line 9, in <module>
my_x.someMethod()
File "slot.py", line 6, in someMethod
self.sumName = "bye"
AttributeError: 'x' object has no attribute 'sumName'

Skip
 
T

Terry Hancock

I've spent hours trying to find a bug that was a simple
spelling mistake.

You're not the first. ;-)
in an init method I declare a variable self.someLongName

later in a different method of the class I use
self.sumLongName
Now I really meant self.someLongName.
In fact I don't want a variable called sumLongName.
Frankly how are you ever to know if this type of error is
occuring?

Both "unit tests" and "interfaces" are useful for catching
simple errors like this one. There is a 'unittest' module
in the Python standard library, and 'interface' modules are
available from the Zope 3 project and PyProtocols, both are
good.

Once you know you have a problem like this (or you've
decided to change a spelling), you can track down the
problem using grep, fix it with sed or use Python itself to
do the job if you really want to.

I once misspelled a name in a set of API methods, which was
very embarrassing (I spelled "fovea" as "fovia"). So then I
had to go through and change it, and report it in as an API
change so that a "patch" became a "minor version upgrade".
Duh. Next time I use a dictionary before freezing an API!

Cheers,
Terry
 
S

Steven D'Aprano

I once misspelled a name in a set of API methods, which was
very embarrassing (I spelled "fovea" as "fovia"). So then I
had to go through and change it, and report it in as an API
change so that a "patch" became a "minor version upgrade".
Duh. Next time I use a dictionary before freezing an API!


Can you please explain what you mean by that? Use a dictionary how?
 
P

Paul Rubin

Steven D'Aprano said:
Can you please explain what you mean by that? Use a dictionary how?

Use a dictionary by looking up words in it to check the spelling.
 
S

skip

Paul> Use a dictionary by looking up words in it to check the spelling.

I think the most widely spread API misspelling must be "referer", as in the
HTTP_REFERER header in the HTTP spec:

http://en.wikipedia.org/wiki/Referer

In fact, googling for "referer" and "referrer" reports a similar number of
hits, unlike most misspellings.

Skip
 
T

Terry Hancock

On Fri, 6 Jan 2006 19:51:22 -0600
Paul> Use a dictionary by looking up words in it to
check the spelling.

I think the most widely spread API misspelling must be
"referer", as in the HTTP_REFERER header in the HTTP spec:

http://en.wikipedia.org/wiki/Referer

In fact, googling for "referer" and "referrer" reports a
similar number of hits, unlike most misspellings.

You know, I almost mentioned that myself. Drives me crazy.

One thing I've figured out is that using the full spelling
of a word instead of groovy programmer abbreviations makes
it a lot easier to remember the names of things. Of course,
like everything, that can be taken too far, so I still use
things like "bkg_clr" too. Old habits are hard to break.
 
S

skip

Terry> You know, I almost mentioned that myself. Drives me crazy.

Me too. I'm one of those people who, for better or worse, is a good
speller. Words just look right or wrong to me and it bothers me when they
look wrong.

Terry> One thing I've figured out is that using the full spelling
Terry> of a word instead of groovy programmer abbreviations makes
Terry> it a lot easier to remember the names of things. Of course,
Terry> like everything, that can be taken too far, so I still use
Terry> things like "bkg_clr" too. Old habits are hard to break.

I do tend to be a bit brief with my names and recognizing an identifier as
an abbreviation don't bother me the way a misspelled word does. Maybe I've
been using Unix systems for too long with their brief command names like mv
and grep. I prefer not to write big_honkin_long_names any more than I have
to either.

Skip
 
S

Sam Pointon

Terry> You know, I almost mentioned that myself. Drives me crazy.

Me too. I'm one of those people who, for better or worse, is a good
speller. Words just look right or wrong to me and it bothers me when they
look wrong.

What's worse is the closely related problem of British/American
English, though you sort of get used to it after typing
s/colour/color/g or s/serialise/serialize/g for the thousandth time.
The words look wrong to me, but they're correct in the context.
 
P

Peter Hansen

Sam said:
What's worse is the closely related problem of British/American
English, though you sort of get used to it after typing
s/colour/color/g or s/serialise/serialize/g for the thousandth time.
The words look wrong to me, but they're correct in the context.

Hah! Canucks r00l! Most of those words look about equally good to us
most of the time. (And it's not because of the beer!) (But our beer
r00lz too.)

-Peter "Freezing-my-ass-off-in-Uxbridge" Hansen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top