hidden features of Python笔记
最近看了关于python的两个很不错的资料
做一下第二个的笔记 加深印象
Quick links to answers:
- Argument Unpacking '*' 不就是c语言里取指针的值 直接把list和dictionary里的值变成函数的参数了 但实际很少用到把 ```python def draw_point(x, y): # do some magic
point_foo = (3, 4) point_bar = {‘y’: 3, ‘x’: 2}
draw_point(*point_foo) draw_point(**point_bar)
</br>
<li><a href="http://stackoverflow.com/questions/101268/hidden-features-of-python#112303">Braces</a></li>
运行的结果:
from __future__ import braces
SyntaxError: not a chance
貌似是个玩笑, 想要引入c语言style的花括号
结果是not a chance、、
```python
from __future__ import braces
@print_args def write(text): print text
write(‘foo’) Arguments: (‘foo’,) {} foo
<li><a href="http://stackoverflow.com/questions/101268/hidden-features-of-python#113198">Default Argument Gotchas / Dangers of Mutable Default arguments</a></li>
I found this a lot easier to understand when I learned that the default arguments live in a tuple that's an attribute of the function,
e.g. foo.func_defaults. Which, being a tuple, is immutable.
还是不太明白
```python
>>> def foo(x=[]):
... x.append(1)
... print x
...
>>> foo()
[1]
>>> foo()
[1, 1]
>>> foo()
[1, 1, 1]
Instead, you should use a sentinel value denoting "not given" and replace with the mutable you'd like as default:
>>> def foo(x=None):
... if x is None:
... x = []
... x.append(1)
... print x
>>> foo()
[1]
>>> foo()
[1]
.get
valueimport this
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!
__missing__
items.pth
filestry/except/else
print()
functionwith
statement