Python类中的修饰@classmethod 和@staticmethod【Flask】

2023-05-25 14:00:24 浏览数 (1)

在flask中,经常会为了代码美观将过程封装成类,去引用这个文件, 用例子来看区别(注意看fetch_one方法的调用

@classmethod:

代码语言:javascript复制
class SQLHelper:

    @staticmethod
    def open():
        conn = pymysql.connect(host='127.0.0.1',port=3306, user='root',passwd='123456',db='flask')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        print('连接成功')
        return cursor, conn

    @staticmethod
    def close(cursor,conn):
        conn.commit()
        cursor.close()
        conn.close()

    @classmethod
    def fetch_one(cls, sql, args):
        cursor, conn = cls.open()
        cursor.execute(sql, args)
        obj = cursor.fetchone()
        cls.close(cursor, conn)
        return obj

    @classmethod
    def fetch_all(cls, sql, args):
        cursor, conn = cls.open()
        cursor.execute(sql, args)
        obj = cursor.fetchall()
        cls.close(cursor, conn)
        return obj


if __name__ == '__main__':

    sql = "select * from users where user=%s and pwd =%s"
    # s = SQLHelper()
    obj = SQLHelper.fetch_one(sql, ['cold','123456'])
    print(obj)

@staticmethod:

代码语言:javascript复制
class SQLHelper:

    @staticmethod
    def open():
        conn = pymysql.connect(host='127.0.0.1',port=3306, user='root',passwd='123456',db='flask')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        print('连接成功')
        return cursor, conn

    @staticmethod
    def close(cursor,conn):
        conn.commit()
        cursor.close()
        conn.close()

    @staticmethod
    def fetch_one( sql, args):
        cursor, conn = SQLHelper.open()
        cursor.execute(sql, args)
        obj = cursor.fetchone()
        SQLHelper.close(cursor, conn)
        return obj

    @staticmethod
    def fetch_all( sql, args):
        cursor, conn = SQLHelper.open()
        cursor.execute(sql, args)
        obj = cursor.fetchall()
        SQLHelper.close(cursor, conn)
        return obj


if __name__ == '__main__':

    sql = "select * from users where user=%s and pwd =%s"
    # s = SQLHelper()
    obj = SQLHelper.fetch_one(sql, ['cold','123456'])
    print(obj)

可以看到,以上这两种都不用实例化

常规的:

代码语言:javascript复制
class SQLHelper:

    @staticmethod
    def open(self):
        conn = pymysql.connect(host='127.0.0.1',port=3306, user='root',passwd='123456',db='flask')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        print('连接成功')
        return cursor, conn

    
    def close(self,cursor,conn):
        conn.commit()
        cursor.close()
        conn.close()


    def fetch_one( self,sql, args):
        cursor, conn = self.open()
        cursor.execute(sql, args)
        obj = cursor.fetchone()
        self.close(cursor, conn)
        return obj


    def fetch_all( self,sql, args):
        cursor, conn = self.open()
        cursor.execute(sql, args)
        obj = cursor.fetchall()
        self.close(cursor, conn)
        return obj


if __name__ == '__main__':

    sql = "select * from users where user=%s and pwd =%s"
     s = SQLHelper()
    obj = s.fetch_one(sql, ['cold','123456'])
    print(obj)

cls:传递的是类本身,self传递的是类的实例(即,对象),加了修饰以后,可以直接将类实例化

0 人点赞