Reputation: 353
I try to execute some python code but i face a problem with passing the parameters. My python code is the following:
#!/usr/bin/python
import MySQLdb
class Sim(object):
def print_db_parameters(self):
print "Host = %s" %self.host
print "User = %s" %self.user
print "Password = %s" %self.password
print "Database = %s" %self.database
def main():
host = "localhost"
user = "root"
password = "root"
database = "sim"
sim_test = Sim(host,user,password,database)
sim_test.print_db_parameters()
if __name__ == "__main__":
main()
When i run it, i receive the following error:
Traceback (most recent call last):
File "Sim.py", line 21, in <module>
main()
File "Sim.py", line 17, in main
sim_test = Sim(host,user,password,database)
TypeError: object.__new__() takes no parameters
Do you have any idea?
Upvotes: 0
Views: 1931
Reputation: 55952
to follow up mipadi with example: It would probably be very helpful to read some tutorials on object oriented programming in python http://docs.python.org/2/tutorial/classes.html
class Sim(object):
def __init__(self, host, user, password, database):
self.host = host
self.user = user
self.password = password
self.database = database
def print_db_parameters(self):
print "Host = %s" %self.host
print "User = %s" %self.user
print "Password = %s" %self.password
print "Database = %s" %self.database
Upvotes: 1
Reputation: 10740
You are passing parameters to a class constructor
sim_test = Sim(host,user,password,database)
But not accepting them. You must create an __init__
method to deal with them.
#!/usr/bin/python
import MySQLdb
class Sim(object):
def __init__(self, host, user, password, database): #New method!!
self.host = host
self.user = user
self.password = password
self.database = database
def print_db_parameters(self):
print "Host = %s" %self.host
print "User = %s" %self.user
print "Password = %s" %self.password
print "Database = %s" %self.database
def main():
host = "localhost"
user = "root"
password = "root"
database = "ARISTEIA_vax"
sim_test = Sim(host,user,password,database)
sim_test.print_db_parameters()
if __name__ == "__main__":
main()
Upvotes: 3
Reputation: 410612
You don't have an __init__
method for your class, but you're passing parameters to the constructor. You should create an __init__
method that accepts parameters.
Upvotes: 3