Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PyCon2013China_ZhuHai_laiyonghao
Search
Zoom.Quiet
December 08, 2013
Technology
2
1k
PyCon2013China_ZhuHai_laiyonghao
PyCon2013China 珠海场 | GDG Livin ZhuHai Life;-)
http://zhgdg.gitcafe.com/2013-12/et-pycon/
Zoom.Quiet
December 08, 2013
Tweet
Share
More Decks by Zoom.Quiet
See All by Zoom.Quiet
PyCon2014China-Zhuhai-high performance
zoomquiet
0
130
PyCon2014China-Zhuhai-meta programming
zoomquiet
1
100
PyCon2014China-Zhuhai-bpm.py
zoomquiet
0
80
PyCon2014China-Zhuhai-luna kv db
zoomquiet
0
84
PyCon2014China-Zhuhai-seed studio
zoomquiet
0
65
PyCon2014China-Zhuhai-Docker Registry Build By Python
zoomquiet
0
78
PyCon2014China-Zhuhai-jeff
zoomquiet
0
58
PyCon2014China-Zhuhai-pythonic front-end
zoomquiet
0
87
DevFest2014-Zhuhai-Polymer
zoomquiet
0
370
Other Decks in Technology
See All in Technology
Oracle Cloudの生成AIサービスって実際どこまで使えるの? エンジニア目線で試してみた
minorun365
PRO
4
280
DevOps視点でAWS re:invent2024の新サービス・アプデを振り返ってみた
oshanqq
0
180
UI State設計とテスト方針
rmakiyama
2
390
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
220
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
300
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
240
ブラックフライデーで購入したPixel9で、Gemini Nanoを動かしてみた
marchin1989
1
520
ハイテク休憩
sat
PRO
2
140
スタートアップで取り組んでいるAzureとMicrosoft 365のセキュリティ対策/How to Improve Azure and Microsoft 365 Security at Startup
yuj1osm
0
210
Storage Browser for Amazon S3
miu_crescent
1
130
祝!Iceberg祭開幕!re:Invent 2024データレイク関連アップデート10分総ざらい
kniino
2
250
Turing × atmaCup #18 - 1st Place Solution
hakubishin3
0
470
Featured
See All Featured
A Tale of Four Properties
chriscoyier
157
23k
Six Lessons from altMBA
skipperchong
27
3.5k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
A designer walks into a library…
pauljervisheath
204
24k
Documentation Writing (for coders)
carmenintech
66
4.5k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Mobile First: as difficult as doing things right
swwweet
222
9k
Speed Design
sergeychernyshev
25
670
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
Facilitating Awesome Meetings
lara
50
6.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Transcript
+ 论 Python 与设计模式 赖勇浩(http://laiyonghao.com) 2013-‐12-‐8
珠海
+ 设计模式?
+ 自我介绍 n 赖勇浩 n 从业 8
年多,主要编程语言是 Python n game -‐> webgame -‐> web n 常在珠三角技术沙龙出没 n http://laiyonghao.com 3
+ PyCon 的老朋友 4
+ 好,正式开始吧! 5
+ 先热热场子…… n 写 Python 代码赚钱的,有没有? 6
+ 先热热场子…… n 写 Python 代码赚钱的,有没有? n
写 Python 超过 1 年的,有没有? 7
+ 再热热场子…… n 读过《设计模式——可复用面向对 象软件的基础》这本书的有没有? 8
+ 再热热场子…… n 读过《设计模式——可复用面向对 象软件的基础》这本书的有没有? n 读过《Head
First 设计模式》 的有没有? 9
10 一个观点
+ 我也希望是这样…… 但事实是…… 11
+ 先看事实:工厂函数(方法) int(…) float(…) type(name, bases,
dict) >>> class X(object): ... a = 1 ... >>> X = type('X', (object,), dict(a=1)) collections.namedtuple() datatime.fromtimestamp(…) Decimal.from_float(…) Fraction.from_float(…) Fraction.from_decimal(…)
+ 先看事实:享元(FlyWeight) >>> i = 10 >>>
j = 5 + 5 >>> id(i) 140479275503872 >>> id(j) 140479275503872 >>> a = 'ab' >>> b = 'a' + 'b' >>> id(a) == id(b) True 13
+ 先看事实:享元(FlyWeight) >>> i = 10 >>>
j = 5 + 5 >>> id(i) 140479275503872 >>> id(j) 140479275503872 >>> a = 'ab' >>> b = 'a' + 'b' >>> id(a) == id(b) True >>> a = a * 10 >>> intern(a) 'abababababababababab' >>> b = 'abababababababababab’ >>> c = 'abababababababababa' + 'b’ >>> d = 'ab' * 10 >>> id(a) == id(b) == id(c) == id(d) True 14
+ 先看事实:适配器 import SocketServer class ThreadedTCPServer(SocketServer.ThreadingMixIn,
SocketServer.TCPServer): pass 15
+ 先看事实:代理模式 >>> import weakref >>> class
A(object):pass ... >>> a = A() >>> a.attr = 1 >>> a.attr 1 >>> a1 = weakref.proxy(a) >>> a1.attr 1 >>> a1 <weakproxy at 0x10dc375d0 to A at 0x10dc3a410> 16
+ 先看事实:模板方法 import SocketServer class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self): self.data = self.request.recv(1024).strip() print "{} wrote:".format(self.client_address[0]) print self.data self.request.sendall(self.data.upper()) 17
+ 先看事实:模板方法 From abc import ABCMeta class
C: __metaclass__ = ABCMeta @abstractmethod def my_abstract_method(self, ...): ... 18
+ 所以,真相是…… n 标准库都在用…… n 标准库都推荐用…… n 怎么可以说不需要?!
19
+ 所以我们不是不需要模式…… 而是要 Pythonic 的模式实现…… 20
+ 不 Pythonic 的设计模式:单例 class Singleton(object):
def __new__(cls, *args, **kw): if not hasattr(cls, '_instance'): orig = super(Singleton, cls) cls._instance = orig.__new__(cls, *args, **kw) return cls._instance 21
+ 单例的三个需求: n 只能有一个实例; n 它必须自行创建这个实例;
n 它必须自行向整个系统提供这个 实例。 22
+ 单例遇上并行 class Singleton(object):
objs = {} objs_locker = threading.Lock() def __new__(cls, *args, **kv): if cls in cls.objs: return cls.objs[cls] 23
+ 单例遇上并行
cls.objs_locker.acquire() try: if cls in cls.objs: return cls.objs[cls] cls.objs[cls] = object.__new__(cls) finally: cls.objs_locker.release() 24
+ Pythonic 的设计模式:单例 n 重新审视 Python 的语法元素,尽量利用已有基础设施。
n 模块 n 所有的变量都会绑定到模块; n 模块只初始化一次; n import 机制是线程安全的(保证了在并发状态下模块也只有一个实 例); n 惯用法: n 文件名首字母大写,如 Moon.py n __all__ 25
+ 不 Pythonic 的设计模式:装饰器 26
+ 代码大概是这样的 class darkroast(Beverage): def cost(self):return
0.99 … class Whip(Beverage): def __init__(self, beverage): self._beverage = beverage def cost(self): return self._beverage.cost() + 0.1 print Whip(Mocha(darkroast())).cost() 27
+ Pythonic 的设计模式:装饰器 def beverage(cost): def
_(orig = 0.0): return orig + cost return _ darkroast = beverage(0.99) whip = beverage(0.1) mocha = beverage(0.2) print whip(mocha(darkroast())) 28
+ 其它设计模式:Borg(MonoState) n 保持对象的唯一性并不重要,只 要共享状态就行 n Alex
Martelli n http:// code.activestate.com/ recipes/66531/ 29
+ 其它设计模式:Borg(MonoState) class Borg(object):
_state = {} def __new__(cls, *args, **kw): ob = super(Borg, cls).__new__(cls, *args, **kw) ob.__dict__ = cls._state return ob 30
+ 动态语言特有的设计模式:mixin n 动态地改变实例的类型的基类,在不修改生成实例过程的情况下, 给实例增加(改变)功能。可用以实现插件框架。 class Bird(object):pass
bird = Bird() class FlyMixin: def fly(self):print 'I can fly.' bird.__class__.__bases__ += (FlyMixin, ) bird.fly() 31
+ 动态语言特有的模式:duck typing n 一个对象有效的语义,不是由继承自特定的类或实现特定的接口, 而是由当前方法和属性的集合决定。 n
当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子, 那么这只鸟就可以被称为鸭子。(James Whitcomb Riley) n 干掉模板方法? n 不,模板方法是想要保证子类实现接口 32
+ 利用设计模式提供更好的接口 n getopt n optparse
n argparse n docopt n Command-‐line interface description language n define interface for your command-‐line app, and n automatically generate parser for it. n 解释器模式 33
+ docopt 34
+ 解释器模式的应用 n GM 指令 player 0
money 10000 player 0 attack 10000 monster 0 die scene monsters die n 过场剧情脚本 monster 0 spawn 0,0 monster 0 moveto 0,-‐10 monster 0 attack monster 0 moveto 0,0 35
+ Q&A
[email protected]
36