[Tutor] Classes

Timothy M. Brauch tbrauch@mindless.com
Tue Feb 25 13:39:08 2003


From: Kenneth Boehme
>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=5):
                ^^^^^
It looks like the problem is here.  You need 2 underscores before and after
the 'special' functions.  It should be _ _ i n i t _ _, not _ i n i t _.

>                self.radius=rad
>
>        def area(self):
>                return self.radius * self.radius * 3.14
>
>I get the following error message when I try to use it:
>
>>>> x=circ.Circle(10)
>Traceback (most recent call last):
>  File "<interactive input>", line 1, in ?
>TypeError: this constructor takes no arguments
>>>> from circ import Circle
>>>> y=Circle(10)
>Traceback (most recent call last):
>  File "<interactive input>", line 1, in ?
>TypeError: this constructor takes no arguments
>
>What I'm doing wrong?
>
>Thanks,
>
>
>Kenneth

I think that might fix the problem.  I'm not sure the documentation makes
the distinction clear.  It is hard, especially if you are using a variable
width font when reading, to distinguish between _ and __.  I'm not sure how
to notice this, other than doing what you did.  I can't think of any other
examples, other than the 'special' functions like these, and as far as I
know, all of them use 2 underscores.

 - Tim

Let's see if I can send this one to everyone, and when I am actually
finished with what I am trying to type.