Attribute error

Rustom Mody rustompmody at gmail.com
Mon Jan 19 06:37:35 CET 2015


On Monday, January 19, 2015 at 5:02:01 AM UTC+5:30, Steven D'Aprano wrote:
> Mahendra Prajapati wrote:
> 
> > Hello,
> > I'm facing this problem with python class, while practising the python
> > programming language.It creates an attribute error. I use windows 7 OS. i
> > don't why.I just need to know why it gives such an error.please let me
> > know. Regards
> 
> An attribute error is a bug in your code. Fix the bug.
> 
> It might be a spelling error:
> 
> s = "Hello World!"
> s.uper()  # Oops, I meant "upper"
> 
> or it might be that you have the wrong object:
> 
> alist = [2, 4, 1]
> alist = alist.sort()  # Oops, replaces alist with None
> alist.index(2)  # None has no index method.
> 
> Either way, it is a bug in your code that needs to be fixed.

Most often it is because you got lost in a maze of dots. ie
x.y.attr works
but you wrote
x.attr
or
a.y.z.attr

Your tools for figuring out (apart from following what Chris suggested)

1. Use the interpreter
2. Use help
3. Use dir
4. Use type

Like so
>>> l=[1,2]
>>> d={1:2}
>>> dir(d)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
>>> type(l)
<type 'list'>

>>> help(l)
[Not shown]
>>> help(l.append)
[Not shown]



More information about the Python-list mailing list