python 面向对象高级编程
来源:互联网 发布:疯狂原始人 知乎 编辑:程序博客网 时间:2024/05/16 08:48
python 装饰器@property使用
class Screen(): @property def width(self): return self._width pass @width.setter def width(self,value): self._width=value @property def height(self): return self._height @width.setter def height(self,value): self._height=value @property def resolution(self): return self.width*self.heights = Screen()s.width = 1024s.height = 768print(s.resolution)assert s.resolution == 786432, '1024 * 768 = %d ?' % s.resolution
python _slots_使用
限制实例的属性,限制属性仅对当前类的实例有效。
class Student(object): __slots__ = ('name','age')s=Student()s.name = '张三's.age = 2s.sex='boy'
Traceback (most recent call last): File "/home/caidong/developProgram/learnDemo/phantomJsDemo/Slots.py", line 7, in <module> s.sex='ds'AttributeError: 'Student' object has no attribute 'sex'
python 实例动态添加属性和方法
from types import MethodTypeclass Student(): passs=Student()s.age=10def set_age(self,age): self.age = agedef get_age(self): return self.ages.set_age=MethodType(set_age,s)s.get_age = MethodType(get_age,s)s.set_age(1)b=s.get_age()print(s.age)print(b)
python 多继承
Mixin 同时继承多个类
python 定制类 特殊函数的
_str_ __iter__ __getitem__ __getattr__
__call__
python 枚举类
from enum import EnumMonth = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
@unique
装饰器可以帮助我们检查保证没有重复值。
元类
阅读全文
0 0
- python 面向对象高级编程
- Python面向对象高级编程
- python面向对象高级编程
- python 面向对象高级编程
- python-面向对象高级编程
- python之面向对象高级编程
- Python学习----面向对象高级编程
- python 2.7 : 面向对象高级编程
- Python 之 高级面向对象编程 slots
- python入门6面向对象高级编程
- python面向对象-高级编程 笔记
- 面向对象高级编程
- 面向对象高级编程
- 面向对象高级编程
- python学习笔记(七) - 面向对象高级编程
- Python面向对象高级编程——学习笔记
- Python学习 (六 面向对象高级编程)
- Python基础之六面向对象高级编程
- HDU
- 三阶幻方口诀
- 百度前端学院task16总结
- Mac 通过 SSH 远程连接Linux服务器
- protobuf应用与原理详解
- python 面向对象高级编程
- 数据结构 二叉树的性质
- 归并排序
- 第5天
- JS&jQuery初级06
- Ajax学习笔记,原生Ajax,使用XMLHttpRequest读取xml内容
- 集训第五天(2017/8/4):刷搜索题
- 学堂在线-数据结构-PA1-范围查询(Range)-解题报告
- 【鸽巢排序】对数组排序,要求时间复杂为O(n)