The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by kennethb]

I'm just starting with Python classes and I'm having some trouble. I create
the following module:

class Circle:
def _init_(self, rad=5):
self.radius=rad
#self.size=len(data)

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

I import the module and then enter the following:

>>> 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
>>>

Can somebody tell me what I'm not doing correctly?

Thanks
import-bot (20211) [Avatar] Offline
#2
Re: classes
[Originally posted by efm]

> I'm just starting with Python classes and I'm having some trouble. I create
> the following module:
>
> class Circle:
> def _init_(self, rad=5):
> self.radius=rad
> #self.size=len(data)
>
> def area(self):
> return self.radius * self.radius * 3.14
>
> I import the module and then enter the following:
>
> >>> 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
> >>>
>
> Can somebody tell me what I'm not doing correctly?

The _init_ method is misnamed. It should be __init__ (double underscores).
Because of this, it's
not getting called when you create the object.