你可能所不知道的python中有用的特性
时间:2011-03-22
来源:互联网
你可能所不知道的python中有用的特性
1.链式操作符,要注意执行顺序问题,方法是加括号。
Python代码
5.True
复制代码
2.debug regex
Python代码
复制代码
另附name match:
Python代码
4.'Lots'
复制代码
3.generator object
可以节省内存,但是只能使用一次
4.同时yield列表中的项和序号。
Python代码
复制代码
5.iter() can take a callable argument
Python代码
复制代码
The iter(callable, until_value) function repeatedly calls callable and yields its result until until_value is returned.
6.Decorators
下面的评论中有人说这已经不算太hidden了
7.yield中send的使用
8.
Python代码
复制代码
反转
Python代码
复制代码
对比
Python代码
复制代码
9.默认参数可能会变
Python代码
复制代码
Instead, you should use a sentinel value denoting "not given" and replace with the mutable you'd like as default:
Python代码
复制代码
默认参数不过是函数的一个属性foo.func_defaults而已,因此对于list类型的可能会变动
10.defaultdict可以增强dict
Python代码
复制代码
相当于
Python代码
复制代码
对于列表中的每个key,如果该defaultdict中不存在,会使用default_factory来创建该值
11.for..else
对于丑陋的代码
Python代码
复制代码
可以使用下面代码来替代
Python代码
复制代码
else在for循环从来没有break时调用,有人提议这个关键字else实际上用finally更合适。
12.值替换,“,”隐含的构成了一个tuple
Python代码
复制代码
1.链式操作符,要注意执行顺序问题,方法是加括号。
Python代码
5.True
- >>> x = 5
- >>> 10 < x < 20
- False
- >>> x < 10 < x*10 < 100
- True
Python代码
- >>> re.compile("""
- ^ # start of a line
- \[font # the font tag
- (?:=(?P<size> # optional [font=+size]
- [-+][0-9]{1,2} # size specification
- ))?
- \] # end of tag
- (.*?) # text between the tags
- \[/font\] # end of the tag
- """, re.DEBUG|re.VERBOSE|re.DOTALL)
Python代码
4.'Lots'
- >>> p = re.compile(r'(?P<word>\b\w+\b)')
- >>> m = p.search( '(((( Lots of punctuation )))' )
- >>> m.group('word')
- 'Lots'
可以节省内存,但是只能使用一次
4.同时yield列表中的项和序号。
Python代码
- >>> a = ['a', 'b', 'c', 'd', 'e']
- >>> for index, item in enumerate(a): print index, item
Python代码
- def seek_next_line(f):
- for c in iter(lambda: f.read(1),'\n'):
- pass
6.Decorators
下面的评论中有人说这已经不算太hidden了
7.yield中send的使用
8.
Python代码
- a = [1,2,3,4,5]
- >>> a[::2] # iterate over the whole list in 2-increments
- [1,3,5]
Python代码
- >>> a[::-1]
- [5,4,3,2,1]
Python代码
- >>> list(reversed(range(4)))#这种方法返回的是iterator
- [3, 2, 1, 0]
Python代码
- >>> def foo(x=[]):
- ... x.append(1)
- ... print x
- ...
- >>> foo()
- [1]
- >>> foo()
- [1, 1]
- >>> foo()
- [1, 1, 1]
Python代码
- >>> def foo(x=None):
- ... if x is None:
- ... x = []
- ... x.append(1)
- ... print x
- >>> foo()
- [1]
- >>> foo()
- [1]
10.defaultdict可以增强dict
Python代码
- >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
- >>> d = defaultdict(list)
- >>> for k, v in s:
- ... d[k].append(v)
- ...
- >>> d.items()
- [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
Python代码
- >>> d = {}
- >>> for k, v in s:
- ... d.setdefault(k, []).append(v)
11.for..else
对于丑陋的代码
Python代码
- found = False
- for i in foo:
- if i == 0:
- found = True
- break
- if not found:
- print("i was never 0")
Python代码
- for i in foo:
- if i == 0:
- break
- else:
- print("i was never 0")
12.值替换,“,”隐含的构成了一个tuple
Python代码
- 1.>>> a = 10
- 2.>>> b = 5
- 3.>>> a, b
- 4.(10, 5)
- 5.
- 6.>>> a, b = b, a
- 7.>>> a, b
- 8.(5, 10)
作者: 中关村村草 发布时间: 2011-03-22
好多特性都第一次看到,我真是孤陋寡闻了。

作者: greatghoul 发布时间: 2011-03-22
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28