码迷,mamicode.com
首页 > 其他好文 > 详细

反射作业

时间:2017-04-28 23:31:34      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:value   err   isp   log   print   列表   return   attr   默认   

1.基于授权定制自己的列表类型,要求定制的自己的__init__方法,
2.定制自己的append:只能向列表加入字符串类型的值
3.定制显示列表中间那个值的属性(提示:property)
4.其余方法都使用list默认的(提示:__getattr__加反射)
技术分享
class List:
    def __init__(self,x):
        self.seq=list(x)

    def append(self,value):
        if not isinstance(value,str):
            raise TypeError(must be str)
        self.seq.append(value)
    @property
    def mid(self):
        index=len(self.seq)//2
        return self.seq[index]
    def __getattr__(self, item):
        return getattr(self.seq,item)

    def __str__(self):
        print("111")
        return str(self.seq)

l=List([1,2,3])

l.append(1)

print(l.mid)

l.insert(0,123)
答案

 

反射作业

标签:value   err   isp   log   print   列表   return   attr   默认   

原文地址:http://www.cnblogs.com/oyoui/p/6783464.html

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