Updating -- Python知识

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012841352/article/details/59124944


  最近密集使用Python,重又拾起曾经的工具。学的快,忘的也快。大的知识点虽还记得,一些细节已经记不得了。

  使用开发语言coding,只是当作工具使用,不求甚解,理解当然不深刻,自然忘的也快。
  FQA,立此文以记录之,方便查询,不断更新,减少网络查询时间,以求加深理解和加速回忆。

【https://www.python.org/dev/peps/pep-0008/ -- Style Guide for Python Code】

import this

The Zen of Python, by Tim Peters   python哲学/python之禅

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.  虽然这并不容易,因为你不是python之父
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!  命名空间是个好主意,应该多用


“Don't Reinvent the Wheel”--不要重复发明轮子

DRY是指Don't Repeat Yourself特指在程序设计以及计算中避免重复代码,因为这样会降低灵活性、简洁性,并且可能导致代码之间的矛盾。《The Pragmatic Programmer》对这一思想做了很好的阐述。
把一切重复的代码抽象出来。最主要的原因是很好维护,当需要改动时只需要改动一次。
代码复用的层次:函数级别复用,对象级别复用,接口级别的,类库级别复用,框架级别复用。
把固定的部分和变化的部分分离出来。固定的部分分离有利于代码复用,变换的部分分离,在变换发生时容易修改替换。简洁比简单更重要,维护成本高低的决定因素。


--------------------------------------------------------------------------------------------------------------------------

1. Python中的“_”,"__"  

【https://segmentfault.com/a/1190000002611411】

  1)_, 交互解释器中最后一次执行语句的返回结果
  _用作被丢弃 的名称,不作特殊使用
  以“_”开头,弱内部标识,指私有变量或方法,定义为类内部变量或方法,只能在类内部访问。使用from module_name  import * 导入时不会被导入,但可以使用import module_name调用。
   a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

  2)以"_"结尾,是为了使用python关键字作为变量名,在结尾添加"_"以避免与python关键字的命名冲突。

  3)"__A", 避免被子类重写,只能在类内部调用。模块内的成员,表示私有成员,外部无法直接调用。使用dir()命令显示此类方法,显示为 类名__A

  4)"__A__", 表示A是一个只能被python调用的方法 或 用作特殊用途。  magic methods??

 

2. Self: Python中对象方法的定义很怪异,第一个参数一般都命名为self(相当于其它语言的this),用于传递对象本身,而在调用的时候则不必显式传递,系统会自动传递。

 

3. Super: 在super机制里可以保证公共父类仅被执行一次

super(type[, object-or-type])  Return the superclass of type. If the second argument is omitted the super object returned is bound. If the second argument is an object, isinstance(obj, type) must be true. If the second argument is a type, issubclass(type2, type) must be true. super() only works for new-style classes. A typical use for calling a cooperative superclass method

super就是用来调用所继承你类的方法,只是多继承时公共仅被 执行一次,而 父类.方法 的用法,每一次申明都会调用一次父类。

 

4. __ini__: 作用是初始化已实例化后的对象, 实例化子类时,会自动隐式调用超类中已定义的__init__。如果子类重写了__init__,实例化子类时,则不会隐式的再去调用超类中已定义的__init__,因此需要显示调用父类方法,即super(子类名,self). __init__

 

5. MRO: MRO(Method Resolution Order):方法解析顺序。 【http://python.jobbole.com/85685/】
Python语言包含了很多优秀的特性,其中多重继承就是其中之一,但是多重继承会引发很多问题,比如二义性,Python中一切皆引用,这使得他不会像C++一样使用虚基类处理基类对象重复的问题,但是如果父类存在同名函数的时候还是会产生二义性,Python中处理这种问题的方法就是MRO。


6. sys.path: python的搜索模块的路径集,list。【https://docs.python.org/3/using/cmdline.html】

import sys
sys.path.append('c:\\mypythonlib')

7. @ decorator 修饰符/装饰器,将下一行的类或函数作为参数传递到修饰行,因此@修饰的函数必须有参数,而被传递的类或函数将无法单独执行

def t1(a):          # if there is no parameter "a": TypeError: t1() takes 0 positional arguments but 1 was given
    print(1)

def t2(b):
    print(2)

@t1
@t2                   

def t3():           # if there is no t3: SyntaxError: unexpected EOF while parsing
    print(3)        # t3(): TypeError: 'NoneType' object is not callable

# t1(t2(t3())).   @t1: @t2 is t1's parameter, @t2: t3 is t2's parameter, interact
# >> 2 1
 

8. python2.7和python3.5的区别

   1)print 和 print()

   2) /: int 和 float

   3) BIF及functools的引用

   4)iter的next方法实现(2.7__next__, 3.X next())


9.string, tuble, list的slice操作分为正向和逆向,因此表达分为

  1)开始:结束:步长 (步长可以省略,默认值为1),其中的步长与开始与结束的方向要一致,即相同的正负值 

  2)开始::步长,此时的遍历只能正向进行到结束(1)或逆向进行到开始(-1),而不能循环进行

  3)slice结束值为开区间,也就是计算中结束值不在其中

a = [1,2,3] => a[0:3:1] => a[-1:-4:-1] 


10.using Excel

import xlrd
import xlwt

#read
rd = xlrd.open_workbook(filename)  #read file
sh = rd.sheets()[0]   #get sheet data
nrow = sh.nrows  #get row number
row = sh.row(n)  #get row content
cel = row[i].value #get cell value

#write
wr = xlwt.Workbook()    #get file
sh = wr.add_sheet(sheetname,Formate)  #add new sheet with format

sh.write(rowN,columnN,value)
wr.save(filename)

11. __name__ == "__main__"

     1) 直接执行文件,则 __name__指向文件中的__main__,会执行__main__中的所有内容
     2) import该文件时,则__name__指向引用文件本身,只会执行引用文件自身,不会执行__main__中的内容

def test():
    return lambda x,y: x+ y

print(test()(5,6))

if __name__ == "__main__":
    print("This is test: ", test())
    print(__name__)
    print(test()(1,2))
#直接执行
11
This is test:  <function test.<locals>.<lambda> at 0x00000000031D3510>
__main__
3

import t1
from functools import reduce

print("This is Main test: ", t1.test()(8,9))
print(t1.__name__)
#引用文件
11
This is Main test:  17
t1

12. map, filter, reduce

都是通过函数对迭代器进行运算,在py3 map和filter生成为对像,因此输出要用list就行转化;而reduce不能直接使用,需要在functools中引入

  1) map: 对迭代器中的每个元素进行函数运算,并输出map对像,使用list转化后即可输出
  2) filter: 按函数对迭代器中的每个元素进行筛选,并输出filter对像,使用list转化后即可输出
  3) reduce: 需要两个参数,将迭代器中的元素从左至右两两按函数进行运算,输出最终值

from functools import reduce

print(list(map(lambda x: x**2,[1,2,3,4,5])))
print(list(filter(lambda x: x%2 == 0, [1,2,3,4,5])))
print(reduce(lambda x,y: x*y,[1,2,3,4,5]))
#输出结果
[1, 4, 9, 16, 25]
[2, 4]
120


13. set: Build an unordered collection of unique elements. -- 可以使用set进行去除重复


14.list转字符串: ''.join(list)  -- 其中,‘’为分隔符,list中的每个元素应为字符串


15.python函数参数中的 * 和 **

  * 以Tuple形式传递任意个无名参数

  ** 以Dict形式传递任意个有名参数


17. from . import sessions

    “ . ” 指当前文件夹里的初始化,即__init__.py文件。

  如果没有__init__.py,则需要使用 .XXX进行文件引用

18. django.models.admin 
  Model.__str__()
__str__() is a Python "magic method" that defines what should be returned if you call str() on the object. Django uses str(obj) (or the related function, unicode(obj) -- see below) in a number of places, most notably as the value displayed to render an object in the Django admin site and as the value inserted into a template when it displays an object. Thus, you should always return a nice, human-readable string for the object's __str__. Although this isn't required, it's strongly encouraged (see the description of __unicode__, below, before putting __str__ methods everywhere).
    Model.__unicode__()
The __unicode__() method is called whenever you call unicode() on an object. Since Django's database backends will return Unicode strings in your model's attributes, you would normally want to write a __unicode__() method for your model. The example in the previous section could be written more simply as:
If you define a __unicode__() method on your model and not a __str__() method, Django will automatically provide you with a __str__() that calls __unicode__() and then converts the result correctly to a UTF-8 encoded string object. This is recommended development practice: define only __unicode__() and let Django take care of the conversion to string objects when required.


19. CSV(Comma-Separated Values),文件以纯文本形式存储表格数据(数字和文本)。

无需引入第三方库,可以用来做数据的存储。

存在问题UnicodeDecodeError: 'gbk' codec can't decode byte 0x94 in position 48: illegal multibyte sequence。

尝试解决,未果: 1) 文件头添加encoding; 2)打开文件时定义encoding

import csv

with open('filecsv.csv','r') as fp:
    freader = csv.reader(fp)
    rl = fp.readlines()
    for i in range(1,len(rl)):
        [ID,Action, Object, Method, Data, Memo] = map(lambda x: x.ljust(10),rl[i].split(","))
        print(ID,Action,Object,Method,Data,Memo)

20. 模块A定义一无返回值方法a(),在模块B中调用print(a()),输出a()结果和None。输出None原因是python的方法如果没有返回值,默认返回值为None

21. 模块T中方法a()的引用
    import T --> T.a()
    import T as t --> t.a()
    from T import* --> a()

22. 三目运算 ...?...:...
    python中的三目运算有两种方法可以实现
    1)result1 if condition else result2
    2) condition and result1 or result2  -- 有缺陷,具体看API中的FAQ 

23. list的反向输出
    1)使用循环,从len(list)-1 到 0 逐一输出,不改变原有list
    2)list.reverse(), 直接将list反转,改变原有list
    3)list[::-1],以步长为-1输出,不改变原有list\

24.BIF: build in function, 内建函数,指python自身的内置函数

25.ZIP: zip(iter1 [,iter2 [...]]) --> zip object
   Return a zip object whose .__next__() method returns a tuple where the i-th element comes from the i-th iterable argument.The .__next__() method continues until the shortest iterable in the argument sequence is exhausted and then it raises StopIteration.
   解释:以元组形式返回迭代器的对应元素组合,直到最短长度的迭代器匹配完成。 例:
>>> a = [1,2,3,4]
>>> b = [6,7,8,9,0]
>>> c =zip(a,b)
>>> list(c)
[(1, 6), (2, 7), (3, 8), (4, 9)]

26.yield:generator生成器,生成一个迭代器iterator。在函数中使用yield,返回类型为generator,函数作为生成器,返回生成器参数构成的元组
>>> def test(m):
    a = b = 1
    while(a<m):
        yield a
        a,b=b,a+b        
>>> type(test(15))
<class 'generator'>
>>> test(15)
<generator object test at 0x00000000033D8F68>
>>> list(test(15))
[1, 1, 2, 3, 5, 8, 13]
  修改程序
>>> def test(m):
    a = b = 1
    while(a<m):
        yield a,b
        a,b=b,a+b
>>> test(15)
<generator object test at 0x00000000033D8F68>
>>> list(test(15))
[(1, 1), (1, 2), (2, 3), (3, 5), (5, 8), (8, 13), (13, 21)]
 
  27. 偏函数 -- import functoolspartial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.  
  
 
 
--通过partial实现一个新的函数,新函数需要给定指定的参数或关键字,即用指定的参数替代原有函数的参数
import functools

def test(a,b):
    print(a,"-",b)

test(1,2)

t = functools.partial(test,5)
t(6)

print(functools.partialmethod(t))
1 - 2
5 - 6
functools.partialmethod(functools.partial(<function test at 0x000000000336E488>, 5), , )

28. from itertools import islice (迭代器切片)
>>> from itertools import islice
>>> a = '12345'
>>> list(islice(a,0,len(a)))
['1', '2', '3', '4', '5']


猜你喜欢

转载自blog.csdn.net/u012841352/article/details/59124944