Midataur
Midataur

Reputation: 21

This Constructer does not take arguments

In Python I have been trying out classes for the first time. When I use this code I get the error 'This constructed does not take arguments' on line 15. Can someone please tell me what the problem is?

class Triangle:
    def _init_(self,h,b):
        self.h = h
        self.b = b
    author = 'No one has claimed this rectangle yet'
    description = 'None'
    def area(self):
        return (self.h * self.b)/2
    def description(self,text):
        self.description = text
    def author(self,text):
        self.author = text

fred = Triangle(4,5)
print fred.area()

Upvotes: 0

Views: 75

Answers (3)

Nick Taylor
Nick Taylor

Reputation: 174

You have defined your constructor as _init_ when it should be defined as __init__ (note the double underscore). Python is not seeing your __init__ (as it is misnamed), and is just assuming a default constructor (which does not take arguments).

Upvotes: 2

Mark Skelton
Mark Skelton

Reputation: 3891

Your error is in the init function. It is supposed to have two underscores before and after like this __init__().

Here is the correct code:

class Triangle:
    def __init__(self,h,b):
        self.h = h
        self.b = b
    author = 'No one has claimed this rectangle yet'
    description = 'None'
    def area(self):
        return (self.h * self.b)/2
    def description(self,text):
        self.description = text
    def author(self,text):
        self.author = text

fred = Triangle(4,5)
print fred.area()

Upvotes: 0

Selcuk
Selcuk

Reputation: 59219

You should use double underscores __ to denote __init__:

def __init__(self, h, b):

Upvotes: 2

Related Questions