cannot initialise class with _init_ method - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

cannot initialise class with _init_ method

enryenry Posts: 4Member
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

Comments

  • mojave_kidmojave_kid Posts: 3Member
    Hello Enry !

    the class constructor in Python is __init__ (double underscores..hehehe)


    sample

    class CTestClass:

    def __init__(self):
    print "Hello World"



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

  • enryenry Posts: 4Member
    Thanks a lot. I am learning... :-)
    Enry


    : Hello Enry !
    :
    : the class constructor in Python is __init__ (double underscores..hehehe)
    :
    :
    : sample
    :
    : class CTestClass:
    :
    : def __init__(self):
    : print "Hello World"
    :
    :
    :
    : : 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
    : :
    : :
    :
    :

Sign In or Register to comment.