473,545 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Spelling mistakes!

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

in an init method I declare a variable self.someLongNa me

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

Jan 6 '06 #1
53 3927
KraftDiner wrote:
Frankly how are you ever to know if this type of error is occuring?


By the traceback:

modelnine@phoen ix ~ $ 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.
class x(object): .... def __init__(self):
.... self.somename = "hello"
.... def somemethod(self ):
.... print self.sumname
.... a = x()
a.somemethod() Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in somemethod
AttributeError: 'x' object has no attribute 'sumname'


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.
Jan 6 '06 #2
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

Jan 6 '06 #3
try this:

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

find that bug.

Jan 6 '06 #4
KraftDiner wrote:
I've spent hours trying to find a bug that was a simple spelling
mistake.

in an init method I declare a variable self.someLongNa me

later in a different method of the class I use
self.sumLongNam e
Now I really meant self.someLongNa me.
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.
Jan 6 '06 #5
KraftDiner wrote:
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.

Jan 6 '06 #6
KraftDiner wrote:
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

Jan 6 '06 #7
try this:
class x(object):
def __init__(self):
self.someName = "hello"
def someMethod(self ):
self.sumName = "bye" find that bug.


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
Jan 6 '06 #8
On 6 Jan 2006 07:30:41 -0800
"KraftDiner " <bo*******@yaho o.com> wrote:
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.someLongNa me

later in a different method of the class I use
self.sumLongNam e
Now I really meant self.someLongNa me.
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

--
Terry Hancock (ha*****@Anansi Spaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Jan 6 '06 #9
On Fri, 06 Jan 2006 17:23:27 -0600, Terry Hancock wrote:
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?

--
Steven.

Jan 7 '06 #10

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

Similar topics

16
2711
by: English Teacher | last post by:
Which would be a more useful language to learn, Smalltalk or Pearl? Learning curves any different? Thanks!
10
1400
by: gnat | last post by:
Hello c.l.c, Are there any spell checkers out that will go through C code and tell me if i have misspelled anything that is in between quotes (ie. data that may be shown to the end user)? -- gnat.
3
6047
by: Peter | last post by:
Hello! I'm trying to implement a method, that checks spelling of a text and suggests corrections. The C# program looks like: ... Word.Application spellApp = new Word.Application(); Word._Document spellDoc = spellApp.Documents.Add(...); Word.SpellingSuggestion ss; ... if (!spellApp.CheckSpelling(singleWord, ref misVal, ref misVal, ref...
8
1748
by: Nick 'The Database Guy' | last post by:
Hi, I am fully aware of the F7 spell check in access, and I am also aware that this can be automated with the statement, SendKeys "{F7}" However when you do this you are presented with a message box that tells you that the spell check is complete, even if the spell check has
4
1430
by: lyle | last post by:
Sometimes before clicking "Post" I copy my message, open Word and paste. Word is excellent, and can suggest synonyms and translations. But it takes a while for Word to open. Recently, I've been running Outlook 2007 minimized; it opens quickly and gives me the same facility as Word when I paste into a new Mail Message, but considerably faster....
1
1659
by: zafar | last post by:
Hi, I want to make a personal digital library, For that I need make a search engine, User can search by giving the key words, but the spelling of given key word may be incorrect, so It is needed that Interface must suggest the correct spelling (Like in google.com search engine suggest the correct spellings ig the key word is incorrect). so...
2
1697
by: knkk | last post by:
Hi, I came across this perl script someone wrote for spelling suggestions when someone types a wrong spelling. Can someone please convert it to PHP? It will help a lot of people. I am attaching the code as a text file. Source: http://www.norvig.com/spell-correct.html
0
7487
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
7420
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
7680
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. ...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
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
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
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.