Adorable python
-
Upload
rhythm-sun -
Category
Technology
-
view
501 -
download
1
Transcript of Adorable python
Adorable Python
Jerry Peng
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
print “Hello World”
Python First Impression?
Python First Impression
Python First Impression
● 强制缩进– 代码风格一致– 编辑器 Tab/Space 的设置很重要
● 用空格而不用制表符
● 显式声明 self 参数( this )● 动态语言
The Python Language
● 优雅、明确、简单– 用一种办法,最好只用一种办法来做一件事– 良好的可读性
● 通用脚本语言– 胶水语言– 良好的扩展性– 良好的模块化支持– 完全动态,易于编写和调试
The Python Language
● 完全面向对象– 一切都是对象
● 没有 Java 中的基本类型和对象类型之分
– 继承、重载、多态的完整支持● 不鼓励过度抽象的 Java 式设计
The Python Language
● 有限但够用的函数式特性– 匿名函数 lambda
– 高阶函数● 接受函数作为参数● 返回函数
– 内置的函数式风格 API● map/filter/reduce, functools
The Python Language
● 内置数据结构– List
● [item1, item2, item3]
– Tuple● (item1, item2, item3)
– Dict ● {key1: value1, key2: value2}
– Set ● {item1, item2, item3} (Python 3 Only)
The Python Language
● 强大、完整的标准库– 数据结构和算法、数值运算– 文件 / 目录操作– 文件解析 / 网络协议 /XML/HTML
– OS 服务– 多线程 / 多进程– 多媒体– GUI(Tk)
Python and Linux
Python and Linux
● 在 Linux 世界一直很有地位– 大量库都有 Python 绑定
● 试试搜索 Ubuntu 中“ python-” 开头的包
● Perl 的替代品– 文本处理仍不如 Perl ?
● 适合 Linux 玩家实现各种好玩的程序
Linux Apps Powered By Python
● Gentoo Portage● Ubuntu Tweak
● iBus 输入法● Deluge BT 客户端● Exaile 音乐播放器● Mercurial, Bazaar 版本控制系统
Linux Apps Powered By Python
● Frets on Fire
– 跨平台、开源的“吉他英雄”– PyGame, PyOpenGL
– [OT] 喜欢摇滚的同学一定要试试!
Python and the Web
● 2005 年 Ruby on Rails 的兴起– 让人折服的开发效率
● 百花齐放的 Python Web 框架– Django
● Google App Engine
– Web.py
– Pylons → Pyramid
– Tornado● Web Server & Web Framework
Python and the Web
● WSGI– Web Server Gateway Interface
● 几乎所有的 Python Web Framework 都符合WSGI 标准
– 方便选择不同部署方案
Python and the Web
● 豆瓣● Google
– Google 三大语言之一– 2011 Google IO Python Session
● FriendFeed(Facebook)
– Tornado 在此诞生● Reddit
– Pylons
Python and the Web
●国内的 Python 职位大部分都是 Web 开发相关,有志于当 Python 程序员的同学应该有所关注
The Python Community
● CPyUG/Python-CN/ 啄木鸟社区– 国内最好的 Python 社区– 各路高手出没
● Python List
– 小心看不过来● Python Sites
– Http://simple-is-better.com
– Http://www.zhimaq.com
Where To Start
● Books
– Dive Into Python推荐给有编程经验的人– Python核心编程– 简明 Python教程
● Python Online Documentation● Python Shell
– ipython
● Python 2 or 3 ?
Personal Python Experience
● Java 系统的外围工具● Java 系统补丁工具● NAS 设备核心程序● 网络流量监控工具● MPD专辑封面抓取工具● VeryCD资源监视器
意犹未尽有木有?
A Little Deeper into Python
● Python 中的酷特性– 列表解析 List Comprehension
– 迭代器 / 生成器 Iterator/Generator
– 函数的魔法– 装饰器 Decorator
● 关于 Pythonic
– 编码风格
List Comprehension
● 简洁、可读的生成列表的方法– 表达式– for循环——支持嵌套– If 过滤条件– map/filter 合体
Iterators
● 迭代器– 抽象了顺序访问数据的过程– 包含 __iter__ 方法的特殊对象
● __iter__ 返回的对象必须包含 next 方法● next 返回下一个值● 如果结束了,应该抛出 StopIteration异常
Iterators
Iterators
● 遍布 Python 语言的各个角落– For循环– 列表解析– 文件对象– ...
Iterators
● itertools– izip, ifilter, imap
– groupby, combination
Generator
● 用来快速实现 iterator 的工具– 形如普通函数– return变成 yield ,并多次返回
● 协程 (coroutine)
– 可以实现 lazy 运算
Generator
Generator
● 用类似列表解析的方法写 Generator
The Magic of Functions
● 函数是一等公民– 函数也是对象,可以作为函数的参数和返回值
● 匿名函数 lambda ( FP 中叫闭包 closure )– 再也不想碰 Java 的匿名内部类了
● 函数式风格的 API– map, filter, reduce
● functools
The Magic of Functions
● map/filter 可以被列表解析取代● reduce 是用来归并一组数据的
– map/reduce
The Magic of Functions
The Magic of Functions
Decorators
● Decorator装饰器– 接受函数作为参数– 返回包装过的函数
● 类似 Java 中的 AOP (面向切面编程)● 实现元编程
– Web Framework 中常用
Decorators
Decorators
Simple is Better!
● 可读性第一● 保持简单● 再好再酷的特性都不要滥用● 多实践!● Be Pythonic !
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 来看完整的版本● 需要积累和沉淀
Be Pythonic
● What is Pythonic[limodou]
– 简单、清晰– 不要过分强调技巧– 尽量使用 Python 已经提供的功能– 符合 Python 的思维方式
● Pythonic到底是什么玩意儿?[赖勇浩 ]
Be Pythonic
● Python 编码规范– PEP 008 Style Guide for Python Code
– Code Like a Pythonista: Idiomatic Python
Q & A
人生苦短,我用 Python