python相关小技巧整理[持续更新]

1. pdb的很是方便的debug,抛弃print吧~ 参考https://www.ibm.com/developerworks/cn/linux/l-cn-pythondebugger/python

import pdb; pdb.set_trace() 来设置一个断点,在你出错的位置以前,以后能够参考连接中的操做,一步步p来print,n来执行next的方法~linux

 

2. python logging  参考 http://python.jobbole.com/81666/函数

 

3. self 与 cls 的写法用spa

(找到的一些简单直接的解释)debug

只是python中约定的写法,本质上只是一个函数参数而已,没有特别含义。code

任何对象调用方法都会把把本身做为该方法中的第一个参数,传递到函数中。(由于在python中万物都是对象,因此当咱们使用Class.method()的时候,实际上的第一个参数是咱们约定的cls)对象

self is used in instance methods, cls is often used in class methodsblog

When a method is decorated with @classmethod it gets the class passed as its first argument so the most common name for it is cls as it points to the class.ip

 

4. python最小值是-sys.maxint - 1ci

              最大值是sys.maxint+1

 

5. python -m tabnanny qaci_script.py  能够看tab和space的问题

 

6. u'() 来解决decode 的问题

 

7. DocString

形式为'''第一行为标题 中间空一行 下面是解释'''

能够用object.__doc__ 的方式调用,能够使程序更加简单易懂

 

 8. 字符串链接的写法

s = ''

for i in seq:

    s += chr(i) 

 这种写法很差,而比较好的是用join

''.join(chr(i) for i in seq)

 

9. list是可变对象,改变list的函数一般没有返回值

eg. a = []

     a.sort() 没有返回值

可是能够用 sorted(a)

 

10.mutable: list, dict, set, 类实例    immutable: 数值类型, 字符串, tuple 

 

11. Collection 模块,还没填完坑

 

12. if.. else 的用法

anwser = "yes!" if rank == 1 else "no"

 

13. 强大zip!

i = ('a', 'b', 'c')

j = (1, 2, 3)

for m, n in zip(i, j):

    print i,j

# the result is
# a 1
# b 2
# c 3

 

14. args:set   kwargs:dict

相关文章
相关标签/搜索