python创建类,调用类方法时老显示没传进去参数

代码如下:

class Update:
    def __int__(self, table, key, header, date):
        self.table = table
        self.key = key
        self.date = date
        self.header = header
    def update_date(self):
        conn = pymysql.connect(host="localhost", port=3306, user="root", password="123456", charset="utf8",
                       db="冀熙科技管理系统")
        cursor = conn.cursor()
        sql = '''update {0} set {1}='{2}' where id='{3}';'''.format(self.table, self.header, self.date, self.key)
        cursor.execute(sql)
        conn.commit()
        cursor.close()
        conn.close()
a = Update
a.update_date(table='project_information', key='202101', header='step', date='关门')

错误显示为:update_date() got an unexpected keyword argument 'table'

__int__ 改成 __init__

a = Update
a.update_date(table='project_information', key='202101', header='step', date='关门')
改成
a = Update(table='project_information', key='202101', header='step', date='关门')
a.update_date()

你题目的解答代码如下:

class Update:
    def __init__(self, table, key, header, date):
        self.table = table
        self.key = key
        self.date = date
        self.header = header
    def update_date(self):
        conn = pymysql.connect(host="localhost", port=3306, user="root", password="123456", charset="utf8",
                       db="冀熙科技管理系统")
        cursor = conn.cursor()
        sql = '''update {0} set {1}='{2}' where id='{3}';'''.format(self.table, self.header, self.date, self.key)
        cursor.execute(sql)
        conn.commit()
        cursor.close()
        conn.close()


a = Update(table='project_information', key='202101', header='step', date='关门')
a.update_date()

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

应该这样写

import pymysql

class Update:
    def __init__(self, table, key, header, date):
        self.table = table
        self.key = key
        self.date = date
        self.header = header
    def update_date(self):
        conn = pymysql.connect(host="localhost", port=3306, user="root", password="123456", charset="utf8",
                       db="冀熙科技管理系统")
        cursor = conn.cursor()
        sql = '''update {0} set {1}='{2}' where id='{3}';'''.format(self.table, self.header, self.date, self.key)
        cursor.execute(sql)
        conn.commit()
        cursor.close()
        conn.close()
a = Update(table='project_information', key='202101', header='step', date='关门')
a.update_date()

详细可参考 类的创建及使用

__init__写成__int__
有帮助望采纳~