Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83998 > unrolled thread

Attribute error

Started byMahendra Prajapati <prajapatimahendra@ymail.com>
First post2015-01-18 20:50 +0000
Last post2015-01-18 21:37 -0800
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Attribute error Mahendra Prajapati <prajapatimahendra@ymail.com> - 2015-01-18 20:50 +0000
    Re: Attribute error Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-18 23:07 +0000
    Re: Attribute error Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-19 10:31 +1100
      Re: Attribute error Rustom Mody <rustompmody@gmail.com> - 2015-01-18 21:37 -0800

#83998 — Attribute error

FromMahendra Prajapati <prajapatimahendra@ymail.com>
Date2015-01-18 20:50 +0000
SubjectAttribute error
Message-ID<mailman.17843.1421614370.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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 

[toc] | [next] | [standalone]


#84000

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-01-18 23:07 +0000
Message-ID<m9hebi$94k$1@dont-email.me>
In reply to#83998
On Sun, 18 Jan 2015 20:50:49 +0000, 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

The python docs https://docs.python.org/2/library/exceptions.html say the 
following about AttributeError:

exception AttributeError

Raised when an attribute reference (see Attribute references) or 
assignment fails. (When an object does not support attribute references 
or attribute assignments at all, TypeError is raised.)

Given your description of the problem, the best guess I can make is that 
you are trying to reference a non existent attribute, possibly because 
you have mistyped the name of the attribute you are trying to access.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#84001

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-01-19 10:31 +1100
Message-ID<54bc4264$0$12993$c3e8da3$5496439d@news.astraweb.com>
In reply to#83998
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.




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#84003

FromRustom Mody <rustompmody@gmail.com>
Date2015-01-18 21:37 -0800
Message-ID<7d078078-6019-4fa7-af05-3e5e914863d4@googlegroups.com>
In reply to#84001
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]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web