python基础语法19 面向对象总结,pickle保存对象注意事项

面向对象的三大特性: 继承,封装,多态

多态的三种表现形式:鸭子类型,继承父类,继承抽象类

 

pickle保存对象注意事项

class Foo:
    y = 20

    def __new__(cls, *args, **kwargs):
        print('从文件中读取对象,是否会执行__new__???')
        return object.__new__(cls)

    def __init__(self, x):
        self.x = x

    def func(self):
        pass


f_obj = Foo(100)
# print(f_obj.__dict__)
# print(f_obj.func)

import pickle
# 开始将对象写文件中
with open('tank_handsome', 'wb') as f:
    pickle.dump(f_obj, f)

# 从文件中读取对象
with open('tank_handsome', 'rb') as f:
    obj = pickle.load(f)    #实际上会寻找程序中对应的类,如果没有会报错,如果有就按照当前类的内容
    print(obj.__dict__)
    print(obj.func)

 

posted @ 2019-12-04 14:41  战斗小人  阅读(368)  评论(0编辑  收藏  举报