[Tutor] Classes

Kenneth Boehme kenneth_boehme@hotmail.com
Tue Feb 25 13:09:02 2003


This is a multi-part message in MIME format.

------=_NextPart_000_0044_01C2DCBE.0260F200
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I'm just starting with Python and I'm having problems with getting =
classes to work.

I've written this basic class:

 class Circle:
        def _init_(self, rad=3D5):
                self.radius=3Drad

        def area(self):
                return self.radius * self.radius * 3.14

I get the following error message when I try to use it:

>>> x=3Dcirc.Circle(10)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: this constructor takes no arguments
>>> from circ import Circle
>>> y=3DCircle(10)
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: this constructor takes no arguments
>>>=20

What I'm doing wrong?

Thanks,

Kenneth
------=_NextPart_000_0044_01C2DCBE.0260F200
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4922.900" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>I'm just starting with Python and I'm =
having=20
problems with getting classes to work.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I've written this basic =
class:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;class Circle:</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def=20
_init_(self, rad=3D5):</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.radius=3Drad</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT=
=20
face=3DArial size=3D2>def area(self):</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return=20
self.radius * self.radius * 3.14</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I get the following error message when =
I try to use=20
it:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&gt;&gt;&gt; =
x=3Dcirc.Circle(10)<BR>Traceback (most=20
recent call last):<BR>&nbsp; File "&lt;interactive input&gt;", line 1, =
in=20
?<BR>TypeError: this constructor takes no arguments<BR>&gt;&gt;&gt; from =
circ=20
import Circle<BR>&gt;&gt;&gt; y=3DCircle(10)<BR>Traceback (most recent =
call=20
last):<BR>&nbsp; File "&lt;interactive input&gt;", line 1, in =
?<BR>TypeError:=20
this constructor takes no arguments<BR>&gt;&gt;&gt; </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>What I'm doing wrong?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Thanks,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Kenneth</FONT></DIV></BODY></HTML>

------=_NextPart_000_0044_01C2DCBE.0260F200--