您的位置:首页 > 其它

今天的学习

2018-03-09 19:35 134 查看
今天觉得Python的基础知识还不够好,于是又重新翻一遍廖雪峰网站,重点看了高阶函数和面向对象编程。学习map reduce filter sorted 等功能,通过函数作为参数和可迭代对象的组合,功能非常强大;
以及匿名函数(lambda),使用该函数可以起到懒人作用
另外再学习面向对象的方法和属性,get_property()和set_property()方法,采用这种方式防止了了外部代码对对象属性的随意篡改,另外增加了对属性值设置的范围限制。为了使得代码更加的Pythonic,于是引入@property 这个装饰器。

贴上今天的练习代码:

class Screen(object):
@property
def width(self):
return self._width
@width.setter
def width(self,value):
self._width=value
@property
def height(self):
return self._height
@height.setter
def height(self,value):
self._height=value
@property
def resolution(self):
return self._width*self._height
s=Screen()
s.height=1024
s.width=768
print(s.resolution)

结果:
786432
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: