TypeError: this constructor takes no arguments

M

Manoj

Hi ,
I have written a simple program usinga class.
But it not compiling.The error i get is
my = myclass("Paul", "John")
TypeError: this constructor takes no arguments

I checked some previous posting.I have given two underscores
for the constructor it.

Any ideas ?

-----------------------------------------------------
#class.py

class person:
def __init__(self, lname, fname):
self.__lname = lname
self.__fname = fname
def getFirstname(self):
return(self.__fname)
def getLastname(self):
return(self.__lname)
def putFirstname(self, s):
self.__fname = s
def putLastname(self, s):
self.__lname = s
__fname = ""
__lname = ""

#Here we go
my = myclass("Paul", "John")
my.printout("Sample program using CLASS")
my.printout("written by ")
p = person()
 
P

Peter Otten

Manoj said:
I have written a simple program usinga class.
But it not compiling.The error i get is
my = myclass("Paul", "John")
TypeError: this constructor takes no arguments

Please make sure you post the actual code. Your snippet fails with a
NameError:

Traceback (most recent call last):
File "class.py", line 19, in ?
my = myclass("Paul", "John")
NameError: name 'myclass' is not defined

and indeed you defined 'person', not 'myclass'.

Peter
 
M

Manoj

Here is the full program.
--------------------------
class myclass:
def printout(self, string):
print string
class person:
def __init__(self, lname, fname):
self.__lname = lname
self.__fname = fname
def getFirstname(self):
return(self.__fname)
def getLastname(self):
return(self.__lname)
def putFirstname(self, s):
self.__fname = s
def putLastname(self, s):
self.__lname = s
__fname = ""
__lname = ""

#Here we go
my = myclass("Paul", "John")
my.printout("Sample program using CLASS")
my.printout("written by ")
p = person()
#This will return error since the variable is private
#print p.__fname
p.putFirstname("Paul")
p.putLastname("John")

print p.getFirstname(), p.getLastname()
 
J

Jorge Godoy

Here is the full program.
--------------------------
class myclass:
def printout(self, string):
print string
class person:
def __init__(self, lname, fname):
self.__lname = lname
self.__fname = fname
def getFirstname(self):
return(self.__fname)
def getLastname(self):
return(self.__lname)
def putFirstname(self, s):
self.__fname = s
def putLastname(self, s):
self.__lname = s
__fname = ""
__lname = ""

#Here we go
my = myclass("Paul", "John")

This class accepts no parameter for its initialization. You probably meant
the person class...

Try:

class myclass(person):
...


It makes myclass inherits from the person class.
.... def __init__(self, lname, fname):
.... self.__lname = lname
.... self.__fname = fname
........ def printout(self, string):
.... print string
....


Be seeing you,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,779
Messages
2,569,606
Members
45,239
Latest member
Alex Young

Latest Threads

Top