Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot initialize class with _init_ method

Status
Not open for further replies.

Enri

Programmer
May 24, 2003
1
GB
Hy there,

I have tried to create a Class using the method _init_ to initialise new instances.

When I create the object and pass the arguments, I get the TypeError:
the constructor takes no argument.

If I don't pass arguments, the object is created with no error. However when I call a method of the object, I get the AttributeError: the instance has no attribute 'name of attribute'

Here is the code I used to create the class:

class person:
def _init_(self, firstName, lastName):
self.firstName = firstName
self.lastName = lastName

def fullName(self):
return self.firstName + ' ' + self.lastName


Here is the code I run to create the object and print it:
>>> Maria = person('Maria', 'Smith')
TypeError: this constructor takes no arguments

Alternatively:

>>> Maria = person()
>>> Maria.fullName()
AttributeError: person instance has no attribute 'firstname'


What can I do to fix this problem?

Thanks for your help.
Enry



'Maria Smith'



 
[tt]class person:
def __init__(self, firstName, lastName):
self.firstName = firstName
self.lastName = lastName
[/tt]

there should be two underscores before and after 'init'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top