- 博客(4)
- 资源 (3)
- 收藏
- 关注
原创 使用生成器创建新的迭代器
实现一个自定义的迭代模式,使其区别于常见的内建函数(range, reversed等)。#实现生成器def frange(start, stop, increment): x = start while x < stop: yield x x += increment#迭代调用for n in frange(1, 10, 0.5):...
2019-02-27 23:59:17 230
原创 委托迭代
构建了一个自定义容器对象,里面包含有列表、元组或其他可迭代对象。 想直接在这个新容器对象上执行迭代操作。只需要定义一个 _iter_() 方法。class Node: def __init__(self, value): self._value = value self._children = [] def __repr__(self): ...
2019-02-27 23:53:27 192
原创 使用next遍历迭代器
不使用for遍历可迭代对象,而使用 next() 函数并在代码中捕获 StopIteration 异常。 比如,下面的例子手动读取一个文件中的所有行:def manual_iter(): with open('test.txt', "r") as f: try: while True: line = next(f)...
2019-02-27 23:45:47 1955
原创 使用元类控制实例的创建
想通过改变实例创建方式来实现单例、缓存或其他类似的特性。可以定义一个元类并自己实现 _call_() 方法class NoInstances(type): def __call__(self, *args, **kwargs): raise TypeError("Can't instantiate directly")# Exampleclass Spam(meta...
2019-02-20 13:21:24 170
python正则表达式全部方法
2017-09-24
基于django搭建的博客
2017-09-13
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人