我试图根据传递给python程序的命令行参数的数量,在user.py中实例化User类的实例。我得到了TypeError: __init__() takes exactly 4 arguments (3 given)
。这是代码:
#!/usr/bin/python
import sys, argparse
from user import User
if __name__ == "__main__":
# Parse args
parser = argparse.ArgumentParser("Batch insert")
parser.add_argument('-d','--dbfilepath', dest='dbfilepath', help='Absolute path and filename of the sqlite database file', required=False, default='/usr/local/foo/bar.sqlite')
parser.add_argument('-v','--verbose', dest='verbose', help='Verbose output', required=False, action='store_true')
parser.add_argument('-l', '--list', help='comma-delimited list of username password pairs', required=True, type=str)
args = parser.parse_args()
my_list = [str(item) for item in args.list.split(',')]
# Only continue if we have even number of items in the un/pw list
if len(my_list) % 2 == 0:
count = 0
u = User(args.dbfilepath, args.verbose)
while (count < len(my_list)):
u.create(my_list[count], my_list[count+1])
count = count + 1
file user.py:
class User:
def __init__(self, dbfilepath, verbose=False):
self._verbose = verbose
# Open DB and get cursor
self._con = sqlite3.connect(dbfilepath)
self._cursor = self._con.cursor()
执行:
./batch-insert.py -d /tmp/sql.sqlite -v --list stinky,cat
Traceback (most recent call last):
File "./create-users-batch.py", line 15, in <module>
u = User(args.dbfilepath, args.verbose)
TypeError: __init__() takes exactly 4 arguments (3 given)
我做错了什么?