1 / 22

Python 简介

Python 简介. 江健 2011.03.08. 概要. 概念 基础 特性 比较 书单. 关键词. 1991, Guido van Rossum python.org 解释性语言 动态类型 GC GIL 面向对象 可 移植 Open source, free 2.7.1 vs 3.2 快速,高效,整洁 … . 需求. 新技术,新思想 混合编程: 只用 C 或者 Java 走遍天下的时代过去了 动态语言:开发效率, 灵活性 交互式命令行,随时 调试 原型开发,日常任务脚本, web , 互联网,大型在线游戏.

crwys
Download Presentation

Python 简介

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python简介 江健 2011.03.08

  2. 概要 • 概念 • 基础 • 特性 • 比较 • 书单

  3. 关键词 • 1991, Guido van Rossum • python.org • 解释性语言 动态类型 GC GIL • 面向对象 • 可移植 • Open source, free • 2.7.1 vs 3.2 • 快速,高效,整洁 …

  4. 需求 • 新技术,新思想 • 混合编程: • 只用C或者Java走遍天下的时代过去了 • 动态语言:开发效率,灵活性 • 交互式命令行,随时调试 • 原型开发,日常任务脚本,web,互联网,大型在线游戏

  5. 推荐 • Eric raymond, How to Become a Hacker • MIT • Google:主力开发语言, GAE • douban.com > 50% python代码

  6. 优势 • “clean, clarity, well designed, relatively well documented” • 全功能语言 • Web, GUI, 脚本, 嵌入C,Java… • 社区活跃,资源丰富 (库,文档,书,发展) • 简单易学,开发高效 • 小工具 数十行 数分钟 linecount.py 13行 • 小应用 数百行 数小时 spampot.py 3xx行, spoofertest 4xx行 • 风格统一 • 强制缩进 • PEP8 - Style Guide for Python Code • pythonic编码风格

  7. 示例 from hongqiangning@douban #!/usr/bin/env python import os, sys from collections import defaultdict d = defaultdict(int) def main(argv): for dirpath, dirnames, filenames in os.walk(argv[1]): for filename in filenames: path = os.path.join(dirpath, filename) ext = os.path.splitext(filename)[1] d[ext] += len(list(open(path))) for ext, n_lines in d.items(): print ext, n_lines if __name__ == "__main__": main(sys.argv)

  8. 基础 • 内建类型 • Number, String, List, Dictionary, Tuple, File … • 语句 • 没有 switch case, why? • 函数,类 • lambda函数 • 错误处理 • Try catch , exception • 模块化 • Packages(文件夹), modules(文件) • 明确的name space

  9. 特性 • 灵活的语句 c, a, b = a, b, c • 灵活的List和Dictionary • 从一个list 生成另外一个list li= [1, 9, 8, 4, 10, 11, 14] [elemfor elem in li if elem%2] • 一个list列表的去重和排序 names = [“aaa”, “aaa”, “sdfsdfsdf“, “sdfsdfsdfsdf”] names = {}.fromkeys(x).keys() names.sort(cmp=lambda x,y:cmp(len(x), len(y)), reverse=True)

  10. Pythonicfrom hongqiangning@douban

  11. Samples from http://bayes.colorado.edu/PythonIdioms.html Wrong: for s in strings: result += s Right:result = ''.join(strings) Better: if key not in d: d[key] = [] Worse: if not dict.has_key(key): d[key] = [] Worse: result = [] for d in data: if d.Count > 4: result.append[3*d.Count] Better: result = [3*d.Count for d in data if d.Count > 4]

  12. 语言的灵活性 def multiply_by_field(fieldname, multiplier): """Returns function that multiplies field "fieldname" by multiplier.""" def multiplier(x): return getattr(x, fieldname) * multiplier return multiplier triple = multiply_by_field('Count', 3) quadruple = multiply_by_field('Count', 4) halve_sum = multiply_by_field('Sum', 0.5)

  13. generator def permutation(array): if len(array) <= 1: yield array else: for item in permutation(array[1:]): for i in range(len(array)): yield item[:i] + array[0:1] + item[i:] array = [I for I in range(1, 10)] for item in permutation(array): print item ret, nums = rule(item, len(item)/3) if ret: print nums

  14. AOP: Aspect-Oriented Programming • Decorator的核心思想 • 不是业务逻辑,但必不可少的操作 • 日志,计时 ,Cache,访问控制 • 从业务逻辑中解耦合 • 业务逻辑还是单独的模块,函数 • 纵切,插入代码 • 执行前,执行后 • 执行某些函数

  15. 比较 • C, C++, Java, Python • 整体感觉: • C 清晰,简洁,开发效率不高 • C++ pitfalls 紧张 • Java清晰,不简洁,有些笨重 • Python 清晰,简洁 • 组件,项目 • C++,不太活跃,学习曲线高,要求高 • Java • 规范,模块化, 明确的文档 • 时间长了有点烦 • 对基础程序员要求比较低 • Python • 活跃,学习曲线有一些 • 文档不够清楚,主要是动态类型和灵活语法造成的 • 对项目人员要求相对高

  16. 总结 • 非专业程序员 • Python first 甚至 only python • 专业程序员 • 还是C, Java, Python这样的顺序比较好

  17. 书单 • 基础 • Python Tutorials • Learning Python • 可爱的python • Pythonic • PEP 8 • Dive into Python • Python Cookbook • 其他 • Python docs, references • 进阶…

More Related