From: Chris Rebert on 28 Jan 2010 15:16 On Thu, Jan 28, 2010 at 11:57 AM, Ray Holt <mrholtsr(a)sbcglobal.net> wrote: > Why am I getting the following error message. Area has been declared as an > attribute of Circle. Thanks, Ray > > class Circle: > Â def __init__(self): > Â Â Â Â self.radius = 1 > Â def area(self): > Â Â Â Â return self.radius * self.radius * 3.14159 > Â c = Circle() > Â c.radius = 3 > Â print c.area() > > Traceback (most recent call last): > Â File "<pyshell#9>", line 1, in <module> > Â Â Â class Circle: > Â File "<pyshell#9>", line 8, in Circle > Â Â Â print c.area() > AttributeError: Circle instance has no attribute 'area' Unable to reproduce: Python 2.6.4 (r264:75706, Dec 20 2009, 15:52:35) [GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin >>> class Circle: .... def __init__(self): .... self.radius = 1 .... def area(self): .... return self.radius * self.radius * 3.14159 .... >>> c = Circle() >>> c.radius = 3 >>> print c.area() 28.27431 Cheers, Chris -- http://blog.rebertia.com
From: Steve Holden on 28 Jan 2010 15:25 Ray Holt wrote: > Why am I getting the following error message. Area has been declared as > an attribute of Circle. Thanks, Ray > > > class Circle: > def __init__(self): > self.radius = 1 > def area(self): > return self.radius * self.radius * 3.14159 > c = Circle() > c.radius = 3 > print c.area() > > > > Traceback (most recent call last): > File "<pyshell#9>", line 1, in <module> > class Circle: > File "<pyshell#9>", line 8, in Circle > print c.area() > AttributeError: Circle instance has no attribute 'area' > Because you have indented the last three lines to make them a part of the Circle definition. Generally four spaces makes a better indent, and that kind of problem is then much more obvious. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS: http://holdenweb.eventbrite.com/
From: MRAB on 28 Jan 2010 15:33 Ray Holt wrote: > Why am I getting the following error message. Area has been declared as > an attribute of Circle. Thanks, Ray > > > class Circle: > def __init__(self): > self.radius = 1 > def area(self): > return self.radius * self.radius * 3.14159 > c = Circle() > c.radius = 3 > print c.area() > > > > Traceback (most recent call last): > File "<pyshell#9>", line 1, in <module> > class Circle: > File "<pyshell#9>", line 8, in Circle > print c.area() > AttributeError: Circle instance has no attribute 'area' > Probably incorrect indentation. This works: class Circle: def __init__(self): self.radius = 1 def area(self): return self.radius * self.radius * 3.14159 c = Circle() c.radius = 3 print c.area()
|
Pages: 1 Prev: Need help with a program Next: get error install MySQLdb on Mac OS X |