Pythonclassmethodstaticmethod

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pythonclassmethodstaticmethod相关的知识,希望对你有一定的参考价值。

class MySQL:
    def __init__(self,ip,port):
        self.ip=ip
        self.port=port
    @classmethod
    def from_conf(cls):
        import settings
        obj=cls(settings.ip,settings.port)
        return obj
class Student:
    def __init__(self,name,name_id,age,sex,group):
        self.name = name
        self.name_id=name_id
        self.age = age
        self.sex = sex
        self.group=group
    def tell_info(self):
        print(‘‘‘-------%s info
        NAME_ID:%s
        AGE:%s
        SEX:%s
        GROUP:%s
        ‘‘‘%(self.name,self.name_id,self.age,self.sex,self.group))
    def save(self):
        with open(r%s\%s%(settings.student_path,self.name_id),wb)as f:
            res=pickle.dumps(self)
            f.write(res)
    @staticmethod
    def get_all():
        res=os.listdir(settings.student_path) #os.listdir的结果是列表
        for i in res:
            with open(r%s\%s%(settings.student_path,i),rb)as f:
                result=pickle.load(f)
                result.tell_info()

 

以上是关于Pythonclassmethodstaticmethod的主要内容,如果未能解决你的问题,请参考以下文章