码迷,mamicode.com
首页 > 编程语言 > 详细

Pythonclassmethodstaticmethod

时间:2017-07-06 18:34:20      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:class   static   info   load   name   pen   self   dump   stat   

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

标签:class   static   info   load   name   pen   self   dump   stat   

原文地址:http://www.cnblogs.com/c491873412/p/7127292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!