adorable python

43
Adorable Python Jerry Peng

Upload: rhythm-sun

Post on 19-May-2015

510 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Adorable python

Adorable Python

Jerry Peng

Page 2: Adorable python

What We Are Gonna Talk About

● Python First Impression● The Python Language● Python and Linux● Python and the Web● The Python Community● Where to Start

Page 3: Adorable python

print “Hello World”

Python First Impression?

Page 4: Adorable python

Python First Impression

Page 5: Adorable python

Python First Impression

● 强制缩进– 代码风格一致– 编辑器 Tab/Space 的设置很重要

● 用空格而不用制表符

● 显式声明 self 参数( this )● 动态语言

Page 6: Adorable python

The Python Language

● 优雅、明确、简单– 用一种办法,最好只用一种办法来做一件事– 良好的可读性

● 通用脚本语言– 胶水语言– 良好的扩展性– 良好的模块化支持– 完全动态,易于编写和调试

Page 7: Adorable python

The Python Language

● 完全面向对象– 一切都是对象

● 没有 Java 中的基本类型和对象类型之分

– 继承、重载、多态的完整支持● 不鼓励过度抽象的 Java 式设计

Page 8: Adorable python

The Python Language

● 有限但够用的函数式特性– 匿名函数 lambda

– 高阶函数● 接受函数作为参数● 返回函数

– 内置的函数式风格 API● map/filter/reduce, functools

Page 9: Adorable python

The Python Language

● 内置数据结构– List

● [item1, item2, item3]

– Tuple● (item1, item2, item3)

– Dict ● {key1: value1, key2: value2}

– Set ● {item1, item2, item3} (Python 3 Only)

Page 10: Adorable python

The Python Language

● 强大、完整的标准库– 数据结构和算法、数值运算– 文件 / 目录操作– 文件解析 / 网络协议 /XML/HTML

– OS 服务– 多线程 / 多进程– 多媒体– GUI(Tk)

Page 11: Adorable python

Python and Linux

Page 12: Adorable python

Python and Linux

● 在 Linux 世界一直很有地位– 大量库都有 Python 绑定

● 试试搜索 Ubuntu 中“ python-” 开头的包

● Perl 的替代品– 文本处理仍不如 Perl ?

● 适合 Linux 玩家实现各种好玩的程序

Page 13: Adorable python

Linux Apps Powered By Python

● Gentoo Portage● Ubuntu Tweak

● iBus 输入法● Deluge BT 客户端● Exaile 音乐播放器● Mercurial, Bazaar 版本控制系统

Page 14: Adorable python

Linux Apps Powered By Python

● Frets on Fire

– 跨平台、开源的“吉他英雄”– PyGame, PyOpenGL

– [OT] 喜欢摇滚的同学一定要试试!

Page 15: Adorable python

Python and the Web

● 2005 年 Ruby on Rails 的兴起– 让人折服的开发效率

● 百花齐放的 Python Web 框架– Django

● Google App Engine

– Web.py

– Pylons → Pyramid

– Tornado● Web Server & Web Framework

Page 16: Adorable python

Python and the Web

● WSGI– Web Server Gateway Interface

● 几乎所有的 Python Web Framework 都符合WSGI 标准

– 方便选择不同部署方案

Page 17: Adorable python

Python and the Web

● 豆瓣● Google

– Google 三大语言之一– 2011 Google IO Python Session

● FriendFeed(Facebook)

– Tornado 在此诞生● Reddit

– Pylons

Page 18: Adorable python

Python and the Web

●国内的 Python 职位大部分都是 Web 开发相关,有志于当 Python 程序员的同学应该有所关注

Page 19: Adorable python

The Python Community

● CPyUG/Python-CN/ 啄木鸟社区– 国内最好的 Python 社区– 各路高手出没

● Python List

– 小心看不过来● Python Sites

– Http://simple-is-better.com

– Http://www.zhimaq.com

Page 20: Adorable python

Where To Start

● Books

– Dive Into Python推荐给有编程经验的人– Python核心编程– 简明 Python教程

● Python Online Documentation● Python Shell

– ipython

● Python 2 or 3 ?

Page 21: Adorable python

Personal Python Experience

● Java 系统的外围工具● Java 系统补丁工具● NAS 设备核心程序● 网络流量监控工具● MPD专辑封面抓取工具● VeryCD资源监视器

Page 22: Adorable python

意犹未尽有木有?

Page 23: Adorable python

A Little Deeper into Python

● Python 中的酷特性– 列表解析 List Comprehension

– 迭代器 / 生成器 Iterator/Generator

– 函数的魔法– 装饰器 Decorator

● 关于 Pythonic

– 编码风格

Page 24: Adorable python

List Comprehension

● 简洁、可读的生成列表的方法– 表达式– for循环——支持嵌套– If 过滤条件– map/filter 合体

Page 25: Adorable python

Iterators

● 迭代器– 抽象了顺序访问数据的过程– 包含 __iter__ 方法的特殊对象

● __iter__ 返回的对象必须包含 next 方法● next 返回下一个值● 如果结束了,应该抛出 StopIteration异常

Page 26: Adorable python

Iterators

Page 27: Adorable python

Iterators

● 遍布 Python 语言的各个角落– For循环– 列表解析– 文件对象– ...

Page 28: Adorable python

Iterators

● itertools– izip, ifilter, imap

– groupby, combination

Page 29: Adorable python

Generator

● 用来快速实现 iterator 的工具– 形如普通函数– return变成 yield ,并多次返回

● 协程 (coroutine)

– 可以实现 lazy 运算

Page 30: Adorable python

Generator

Page 31: Adorable python

Generator

● 用类似列表解析的方法写 Generator

Page 32: Adorable python

The Magic of Functions

● 函数是一等公民– 函数也是对象,可以作为函数的参数和返回值

● 匿名函数 lambda ( FP 中叫闭包 closure )– 再也不想碰 Java 的匿名内部类了

● 函数式风格的 API– map, filter, reduce

● functools

Page 33: Adorable python

The Magic of Functions

● map/filter 可以被列表解析取代● reduce 是用来归并一组数据的

– map/reduce

Page 34: Adorable python

The Magic of Functions

Page 35: Adorable python

The Magic of Functions

Page 36: Adorable python

Decorators

● Decorator装饰器– 接受函数作为参数– 返回包装过的函数

● 类似 Java 中的 AOP (面向切面编程)● 实现元编程

– Web Framework 中常用

Page 37: Adorable python

Decorators

Page 38: Adorable python

Decorators

Page 39: Adorable python

Simple is Better!

● 可读性第一● 保持简单● 再好再酷的特性都不要滥用● 多实践!● Be Pythonic !

Page 40: Adorable python

Be Pythonic

● The Zen of Python– Beautiful is better than ugly.

– Explicit is better than implicit.

– Simple is better than complex.

– Complex is better than complicated.

● import this 来看完整的版本● 需要积累和沉淀

Page 41: Adorable python

Be Pythonic

● What is Pythonic[limodou]

– 简单、清晰– 不要过分强调技巧– 尽量使用 Python 已经提供的功能– 符合 Python 的思维方式

● Pythonic到底是什么玩意儿?[赖勇浩 ]

Page 42: Adorable python

Be Pythonic

● Python 编码规范– PEP 008 Style Guide for Python Code

– Code Like a Pythonista: Idiomatic Python

Page 43: Adorable python

Q & A

人生苦短,我用 Python