码迷,mamicode.com
首页 > 编程语言 > 详细

Python 最佳实践指南

时间:2015-12-10 11:13:59      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:

粗粗粗略地过了一遍,大体捞了一些东西出来,大段大段英文太费眼了,回头细读在更新进来

浓缩版,20分钟可大体过完,然后根据自己需要去看详细的吧

整体内容还是很不错的,建议细读英文

PS:文档含有巨量的TODO(没写空白着待补充的),不过但从目录上来看还是很强大滴,相信完善后,会成为一份很牛逼的指南(难度比官方指南高一点点)


第零部分 Getting Started

链接

不解释,不翻译,自个看….真的没啥(每本入门书籍第一章…)


第一部分 Writing Great Code

Structuring Your Project

链接

import 最佳实践

Very bad

Better

Best

Python中关于OOP的 观点

Decorators

动态类型(Dynamic typing)

Avoid using the same variable name for different things.

Bad

Good

It is better to use different names even for things that are related, when they have a different type:

可变和不可变类型(Mutable and immutable types)

字符串拼接最佳实践

Bad

Good

Best

join() is not always best
创建新字符串和修改原有字符串

字符串格式化

Code Style

链接

一般概念(General concepts)

明确的代码

Bad

Good

每行一个声明

Bad

Good

函数参数

Avoid the magical wand(这个肿么翻…)

方言(Idioms)

Idiomatic Python code is often referred to as being Pythonic.

列举了一些:

Unpacking

忽略接收变量,这里用的是两个下划线,原因

http://docs.python-guide.org/en/latest/writing/style/#create-an-ignored-variable

同一个元素创建一个长度为N的列表

创建一个长度N的嵌套列表

由列表拼接字符串

快速查找

Zen of Python

PEP8

惯例(Conventions)

判断值是否等于常量

Bad:

Good:

获取字典元素

Bad:

Good:

快捷列表操作

Bad:

Good:

Bad:

Good:

使用enumerate

读文件

Bad:

Good:

超长的行

Bad:

Good:

Reading Great Code

链接

感受下:The number one thing that Python programmers do is read code.

再感受一把:One of the secrets of becoming a great Python programmer is to read, understand, and comprehend excellent code.

几个推荐阅读源代码项目

Howdoi

Flask

Werkzeug

Requests

Tablib

文档(Documentation)

链接

感受一下:Readability is a primary focus for Python developers, in both project and code documentation.

具体还是读原文吧

项目文档组成

几种文档工具

代码文档建议

Comments clarify code and begin with a hash (#).

In Python, docstrings describe modules, classes, and functions:

注解代码块

Do not use triple-quote strings to comment code.

This is not a good practice, because line-oriented command-line tools such as grep will not be aware that the commented code is inactive.

It is better to add hashes at the proper indentation level for every commented line.

最佳实践:

测试你的代码(Testing Your Code)

链接

测试一些通用原则

单元测试(Unittest)

Python内置模块, 文档

文档测试(Doctest)

非精细case,只验证主体功能可用

相关工具

py.text
$ pip install pytest

Nose unittest的扩展
$ pip install nose

tox
$ pip install tox

Unittest2
$ pip install unittest2

mock
$ pip install mock

Common Gotchas(不懂怎么翻…╮(╯▽╰)╭ )

一些新手可能疑惑的例子

两个例子

1.可变默认参数

What You Wrote

What You Might Have Expected to Happen

What Does Happen

What You Should Do Instead

Python默认参数在函数定义处执行一次,而不是每次函数调用时执行。

2.Late Binding Closures(又一个,延迟绑定闭包?)

What You Wrote

What You Might Have Expected to Happen

What Does Happen

What You Should Do Instead

Python的闭包是延时绑定

选择证书(Choosing a License)

链接

开源证书 列表

证书选择器 入口


第二部分 Scenario Guide

都是介绍性质的,类似工具目录,而且大部分是空的,目前没详细信息

要了解具体,goole相关关键词吧

具体自己翻吧 位置

目录:

Network Applications

Http:

Distributed Systems

Web Applications

Context

Frameworks

Web Servers

WSGI Servers

Hosting

Templating

HTML Scraping

Command Line Applications

GUI Applications

Databases

Networking

Systems Administration

Continuous Integration

Speed

C Extensions

Threading

Scientific Applications

Tools

Libraries

Image Manipulation

XML parsing

 

Python 最佳实践指南

标签:

原文地址:http://www.cnblogs.com/fengliucaizi/p/5035027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!