lazy ideas in programming
lazy ideas:
singleton
class eager_meta(type):
def __init__(clz, name, bases, dic):
super(eager_meta, clz).__init__(name, bases, dic)
clz._instance = clz() class singleton_eager(object):
__metaclass__ = eager_meta @classmethod
def instance(clz):
return clz._instance class singleton_lazy(object):
__instance = None
@classmethod
def instance(clz):
if clz.__instance is None:
clz.__instance = singleton_lazy()
return clz.__instance
PS:在python中,这样使用单例模式不是很pythonic,更好的办法可见在stackoverflow上的这篇文章《creating-a-singleton-in-python》。另外在多线程环境下,要实现线程安全的单例还是很复杂的,具体讨论可参见iteye上的分析。
proxy:
- remote proxy(远程代理),如RMI, RPC
- virtual proxy(虚代理),根据需要创建开销很大的对象,如文档中图片的加载
- (保护代理):控制对原始对象的访问, 如智能指针
Short-circuit evaluation:
ret = any(self.calc_and_ret(e) for e in elements)
def self.calc_and_ret(self, e):
# do a lot of calc here which effect self
return True(or False)
generator:
for x in [i*i for i in xrange(10000)]
# do sth with i for x in (i*i for i in xrange(10000)]
# do sth with i
函数式编程语言中的应用:
cache:
class Fruit:
def __init__(self, item):
self.item = item class Fruits:
def __init__(self):
self.items = {} def get_fruit(self, item):
if item not in self.items:
self.items[item] = Fruit(item) return self.items[item] if __name__ == '__main__':
fruits = Fruits()
print(fruits.get_fruit('Apple'))
print(fruits.get_fruit('Lime'))
Dirty Flag:
CopyOnWrite:
web开发中的惰性加载与惰性预加载:
总结:
lazy ideas in programming的更多相关文章
- lazy ideas in programming(编程中的惰性思想)
lazy形容词,懒惰的,毫无疑问是一个贬义词.但是,对于计算机领域,lazy却是非常重要的优化思想:把任务推迟到必须的时刻,好处是避免重复计算,甚至不计算.本文的目的是抛砖引玉,总结一些编程中的laz ...
- Teach Yourself Programming in Ten Years
Teach Yourself Programming in Ten Years——用十年教会自己编程 作者:Peter Norvig 译者:刘海粟 本文原文为:http://norvig.com/21 ...
- [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming
Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II_Easy tag: Dynamic Programming
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, ...
随机推荐
- 超市RFID结算系统项目进度与总结
超市RFID结算系统项目进度与总结 超市RFID结算系统本周末(明天演示),目前进度如下: 一.后台PHP端已经完成了大部分的工作,包括以下: 1.数据库的建立(目前只包括用户表.商品信息表.购物车表 ...
- 使用Emacs中的org-mode写cnblogs之图片插入
.title { text-align: center; margin-bottom: .2em } .subtitle { text-align: center; font-size: medium ...
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- ubuntu内核的编译安装
原创声明:转载请注明出处. 一.操作环境: 1.ubuntu版本 2.linux原有内核版本 3.要安装的linux内核版本 linux-3.16.39 二.新内核的编译和安装 1.首先下载linux ...
- sql 里的 IF ELSE 语句用法
IF ELSE 语句 IF ELSE 是最基本的编程语句结构之一几乎每一种编程语言都支持这种结构而 它在用于对从数据库返回的数据进行检查是非常有用的TRANSACT-SQL 使用IF ELSE 的 ...
- AtCoder Beginner Contest 052 ABCD题
A - Two Rectangles Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement The ...
- UVa 11495 - Bubbles and Buckets
题目大意:给一个有n个数的序列,通过交换相邻的逆序数使这个序列最终有序,求需要交换的次数. 本来可以用冒泡排序解决,但是n达到105,用冒泡排序会超时,用O(nlogn)的归并排序可以达到要求.< ...
- eclipse中集成svn maven开发手册---创建提分支
开发时,需要拉取分支进行修改等操作 右键项目,选择team->分支/标记 输入创建分支地址 注意: 1,创建分支路径时,最后一层文件名称为项目的名称 2,点击浏览按钮可能会出现无法选择,且ecl ...
- centos服务器设置代理上网的方法
这里以centos7.0为例,记录代理服务器设置过程: 1.全局的代理设置: vi /etc/profile 添加下面内容 http_proxy = http://username:password@ ...
- Php连接及读取和写入mysql数据库的常用代码
在这里我总结了常用的PHP连接MySQL数据库以及读取写入数据库的方法,希望能够帮到你,当然也是作为我自己的一个回顾总结. 1.为了更好地设置数据连接,一般会将数据连接所涉及的值定义成变量. $mys ...