最近看了关于 python 的两个很不错的资料
做一下第二个的笔记 加深印象
Quick links to answers:
- Argument Unpacking '*' 不就是 c 语言里取指针的值 直接把 list 和 dictionary 里的值变成函数的参数了 但实际很少用到把
- Braces 运行的结果: from __future__ import braces SyntaxError: not a chance 貌似是个玩笑, 想要引入 c 语言 style 的花括号 结果是 not a chance、、
- Chaining Comparison Operators 连续的比较符吧,实际中还是挺有用的 突然想到的: if A and B in L: → if (A and B) in L:
- Decorators 装饰器,之前也从来没有用过,以后尝试一下 看了一篇装饰器的文章,写的挺好的 大致明白了原理和应用 http://www.cnblogs.com/coderzh/archive/2010/04/27/python-cookbook33-Decorators.html
- Descriptors Python 描述符(descriptor)解密 http://www.geekfan.net/7862/
- Dictionary default
.get
value
前一种如果不包含键值,会报错,后一种会返回第二个参数的值。
- Docstring Tests 不懂啥意思
- Ellipsis Slicing Syntax
- Enumeration
- For/else
- Function as iter() argument
- Generator expressions
import this
这个吊。。
- In Place Value Swapping
- List stepping
__missing__
items- Multi-line Regex 多行正则表达式 有机会要学一下
- Named string formatting
- Nested list/generator comprehensions 双重循环生成 list 炫酷。。
- New types at runtime
.pth
files- ROT13 Encoding
- Regex Debugging
- Sending to Generators 生成器 (generator) 有时间要深入学习一下
- Tab Completion in Interactive Interpreter
- Ternary Expression
try/except/else
- Unpacking+
print()
function with
statement
1 | def draw_point(x, y): |
1 | from __future__ import braces |
1 | 5 x = |
1 | def print_args(function): |
1 | sum[value] = sum.get(value, 0) + 1 |
import this
# btw look at this module's source :)
The Zen of Python, by Tim Peters
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
1 | [(i,j) for i in range(3) for j in range(i) ] |