Python基础31

今日内容概要

  • 单例模式实现的多种方式

  • pickle序列化模块

今日内容详细

单例模式实现的多种方式

class C1:
    __instance = None

    def __init__(self, name, age):
        self.name = name
        self.age = age

    @classmethod
    def singleton(cls):
        if not cls.__instance:
            cls.__instance = cls('jason', 18)
        return cls.__instance


obj1 = C1.singleton()
obj2 = C1.singleton()
obj3 = C1.singleton()
print(id(obj1), id(obj2), id(obj3))
obj4 = C1('kevin', 28)
obj5 = C1('tony', 38)
print(id(obj4), id(obj5))



class Mymeta(type):
    def __init__(self, name, bases, dic):  # 定义类Mysql时就触发
        # 事先先从配置文件中取配置来造一个Mysql的实例出来
        self.__instance = object.__new__(self)  # 产生对象
        self.__init__(self.__instance, 'jason', 18)  # 初始化对象
        # 上述两步可以合成下面一步
        # self.__instance=super().__call__(*args,**kwargs)
        super().__init__(name, bases, dic)

    def __call__(self, *args, **kwargs):  # Mysql(...)时触发
        if args or kwargs:  # args或kwargs内有值
            obj = object.__new__(self)
            self.__init__(obj, *args, **kwargs)
            return obj
        return self.__instance


class Mysql(metaclass=Mymeta):
    def __init__(self, name, age):
        self.name = name
        self.age = age

obj1 = Mysql()
obj2 = Mysql()
print(id(obj1), id(obj2))
obj3 = Mysql('tony', 321)
obj4 = Mysql('kevin', 222)
print(id(obj3), id(obj4))


'''基于模块的单例模式:提前产生一个对象 之后导模块使用'''
class C1:
    def __init__(self, name):
        self.name = name

obj = C1('jason')


def outer(cls):
    _instance = cls('jason', 18)
    def inner(*args, **kwargs):
        if args or kwargs:
            obj = cls(*args, **kwargs)
            return obj
        return _instance

    return inner


@outer  # Mysql=outer(Mysql)
class Mysql:
    def __init__(self, host, port):
        self.host = host
        self.port = port


obj1 = Mysql()
obj2 = Mysql()
obj3 = Mysql()
print(obj1 is obj2 is obj3)  # True

obj4 = Mysql('1.1.1.3', 3307)
obj5 = Mysql('1.1.1.4', 3308)
print(obj3 is obj4)  # False

pickle序列化模块

优势:能够序列化python中所有的类型
缺陷:只能够在python中使用 无法跨语言传输 
 
需求:产生一个对象并保存到文件中 取出来还是一个对象
    
 
"""
需求:产生一个对象并保存到文件中 取出来还是一个对象
"""
class C1:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def func1(self):
        print('from func1')

    def func2(self):
        print('from func2')

obj = C1('jason', 18)

# import json
# with open(r'a.txt','w',encoding='utf8') as f:
#     json.dump(obj, f)
# import pickle
# with open(r'a.txt', 'wb') as f:
#     pickle.dump(obj, f)
# with open(r'a.txt','rb') as f:
#     data = pickle.load(f)
# print(data)
# data.func1()
# data.func2()
# print(data.name)

选课系统需求分析

选课系统
  	 角色:学校、学员、课程、讲师
 	 要求:
    1. 创建北京、上海 2 所学校
    2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
    3. 课程包含,周期,价格,通过学校创建课程 
    4. 通过学校创建班级, 班级关联课程、讲师5. 创建学员时,选择学校,关联班级
    5. 创建讲师角色时要关联学校, 
    6. 提供三个角色接口  
    	6.1 学员视图, 可以注册, 交学费, 选择班级,  
    	6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩   
    	6.3 管理视图,创建讲师, 创建班级,创建课程
    7. 上面的操作产生的数据都通过pickle序列化保存到文件里
posted @ 2022-11-09 20:02  LZJJJ  阅读(24)  评论(0)    收藏  举报