classmethod与staticmethod内置装饰器
classmethod与staticmethod内置装饰器
1、classmethod
是一个装饰器,给在类内部定义方法装饰,将类内部的方法变为 “类的绑定方法”
class DB: __data = "tom is very cute" def __init__(self, user, pwd, role): self.user = user self.pwd = pwd self.role = role @classmethod def check_db(cls, user, pwd, role): # cls指的是类 # 在类方法内部调用类产生一个实例 ---> 对象 obj = cls(user, pwd, role) # 查看数据前必须通过校验 if obj.user == "root" and obj.pwd == "123" and obj.role == "admin": return cls.__data print(DB.check_db("root", "123", "admin"))