博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Python]小甲鱼Python视频第046课(魔法方法:描述符(Property的原理) )课后题及参考解答...
阅读量:5255 次
发布时间:2019-06-14

本文共 4580 字,大约阅读时间需要 15 分钟。

# -*- coding: utf-8 -*-"""Created on Tue Mar 19 22:48:17 2019@author: fengs""""""测试题:0. 请尽量用自己的语言来解释什么是描述符(不要搜索来的答案,用自己的话解释)?    绑定属性的 set & get & del 方法""""""1. 描述符类中,分别通过哪些魔法方法来实现对属性的 get、set 和 delete 操作的?    __get__    __set__    __delete__""""""2. 请问以下代码,分别调用 test.a 和 test.x,哪个会打印“getting…”?class MyDes:    def __get__(self, instance, owner):        print("getting...")class Test:    a = MyDes()    x = atest = Test()test.atest.x#都会打印 getting """"""class MyDes:    def __get__(self, instance, owner):        print("getting...")        return 1class Test:    a = MyDes()    x = atest = Test()print(test.a)print(test.x)""""""3. 请问以下代码会打印什么内容?class MyDes:    def __init__(self, value = None):        self.val = value    def __get__(self, instance, owner):        return self.val - 20    def __set__(self, instance, value):        self.val = value + 10        print(self.val)class C:    x = MyDes()if __name__ == '__main__':  # 该模块被执行的话,执行下边语句。    c = C()    c.x = 10    print(c.x)        20   __set__中打印    0    __get__中打印""""""4. 请问以下代码会打印什么内容?class MyDes:    def __init__(self, value = None):        self.val = value    def __get__(self, instance, owner):        return self.val ** 2class Test:        x = MyDes(3)        def __init__(self):        #self.x = MyDes(3)test = Test()test.xx 并非类属性,test.x的结果将是一个描述符对象"""class MyDes:    def __init__(self, value = None):        self.val = value    def __get__(self, instance, owner):        return self.val ** 2class Test:        x = MyDes(3)        def __init__(self):        pass        #self.x = MyDes(3)#test = Test()#print(test.x)"""动动手(一定要自己动手试试哦~):0. 按要求编写描述符 MyDes:当类的属性被访问、修改或设置的时候,分别做出提醒。程序实现如下:>>> class Test:        x = MyDes(10, 'x')>>> test = Test()>>> y = test.x正在获取变量: x>>> y10>>> test.x = 8正在修改变量: x>>> del test.x正在删除变量: x噢~这个变量没法删除~>>> test.x正在获取变量: x8""" class MyDes0():        def __init__(self,value = None,name = None):        self.value = value        self.name = name    def __get__(self,instance,owner):        print('正在获取变量: %s ' % self.name )        return self.value    def __set__(self,instance,value):        print('正在修改变量: %s' % self.name )        self.value = value    def __delete__(self,instance):        print('正在删除变量: %s' % self.name )        print('噢=.=,这个变量没法删除')        return Noneclass Test0():    x = MyDes0(10,'x')#test0 = Test0()#print(test0.x)#test0.x = 8#del test0.x#print(test0.x)"""1. 按要求编写描述符 MyDes:记录指定变量的读取和写入操作,并将记录以及触发时间保存到文件(record.txt)程序实现如下:>>> class Test:        x = Record(10, 'x')        y = Record(8.8, 'y')>>> test = Test()>>> test.x10>>> test.y8.8>>> test.x = 123>>> test.x = 1.23>>> test.y = "I love FishC.com!">>>"""import timeclass Record():        def __init__(self,value,name):        self.value = value        self.name = name        self.fileSaveName = "record.txt"            def append_line_to_txt(self,txt_line):        with open(self.fileSaveName,'a') as fid:            fid.write(txt_line)                def __get__(self,instance,owner):        now = time.asctime()        txt_line = "%s 变量于北京时间 %s 被读取, %s = %s \n" % (self.name,now,self.name,self.value)        self.append_line_to_txt(txt_line)        return self.value        def __set__(self,instance,value):        now =  time.asctime()        self.value = value        txt_line = "%s 变量于北京时间 %s 被修改, %s = %s \n" % (self.name,now,self.name,self.value)        self.append_line_to_txt(txt_line)        class Test1():    x = Record(10,'x')    y = Record(8.8,'y')test1 = Test1()print(test1.x)print(test1.y)test1.x = 123test1.x = 1.23test1.y = "I love FishC.com!""""2. 再来一个有趣的案例:编写描述符 MyDes,使用文件来存储属性,属性的值会直接存储到对应的pickle(腌菜,还记得吗?)的文件中。如果属性被删除了,文件也会同时被删除,属性的名字也会被注销。举个栗子:>>> class Test:        x = MyDes('x')        y = MyDes('y')        >>> test = Test()>>> test.x = 123>>> test.y = "I love FishC.com!">>> test.x123>>> test.y'I love FishC.com!'产生对应的文件存储变量的值:"""import os import pickleclass MyDes2():    def __init__(self,name):        self.name = name        self.value = None        self.file_name = self.name + '.pkl'    def __get__(self,instance,owner):        with open(self.file_name,'rb') as fid:            self.value = pickle.load(fid)        return self.value    def __set__(self,instance,value):        self.value = value        with open(self.file_name,'wb') as fid:            pickle.dump(self.value,fid)            def __delete__(self,instance):        os.remove(self.file_name)        class Test2():    x = MyDes2('x')    y = MyDes2('y')        test2 = Test2()test2.x = 123test2.y = 'I love Fishc.Com!'print(test2.x)print(test2.y)#del test2.x

  

转载于:https://www.cnblogs.com/alimy/p/10590034.html

你可能感兴趣的文章
关于收费软件
查看>>
javascript之Style物
查看>>
图的深度优先遍历
查看>>
C# 之 提高WebService性能大数据量网络传输处理
查看>>
[bzoj1004] [HNOI2008] Cards
查看>>
原生HttpClient详细使用示例
查看>>
几道面试题
查看>>
Factory Design Pattern
查看>>
如何在vue单页应用中使用百度地图
查看>>
P1192-台阶问题
查看>>
一、使用pip安装Python包
查看>>
3.PHP 教程_PHP 语法
查看>>
Duilib扩展《01》— 双击、右键消息扩展
查看>>
利用Fiddler拦截接口请求并篡改数据
查看>>
python习题:unittest参数化-数据从文件或excel中读取
查看>>
在工程中要加入新的错误弹出方法
查看>>
PS 滤镜— — sparkle 效果
查看>>
网站产品设计
查看>>
代理ARP
查看>>
go 学习笔记(4) ---项目结构
查看>>