學習python

接觸的語言越多,對語言的偏執就越少,愈來愈明白語言只是一種解決問題的工具 - 核心永遠是問題與解決問題的思路。html

個人體驗,從簡練程度上來說,shell腳本優於perl/python/lua,perl/python優於java/C++,因此能用前者解決的問題,就別用後者。java

項目中用到python,前段時間就「系統」的學了下python - 主要也就是把官方的tutorial過了一遍,從學習python的資料來看,個人評價是:python

  • Python Tutorial(http://docs.python.org/2/tutorial/), 簡潔而重點突出,絕對是上品,把這個快速的過一篇,例子所有敲一遍,想說沒入門都難。另外這裏(http://docs.python.org/2/download.html)download下來的,還有關於函數式編程,網絡編程,argparse的專題,都是10來頁的,讀讀挺不錯
  • Learn python the hard way,這個去年過年的時候拷到ipad上讀過幾頁,絕對是給沒有編程經驗的人看的,若是你是個程序員,相信全篇讀下來你會被囉嗦死的 - 別降格去讀這個了。
  • Learn Python,上千頁的大部頭,看着就有壓力,要是沒事就別浪費時間讀了,邊作邊查reference吧。

另外,python提供了絕佳的交互式環境:程序員

$ python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> dir(os)
['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIA
L', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLA
Y', 'P_WAIT', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'UserDict', 'W_OK', 'X_OK', '_Environ', '__all
__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_copy_reg', '_execvpe', '_exists', '_exit',
 '_get_exports_list', '_make_stat_result', '_make_statvfs_result', '_pickle_stat_result', '_pickle_statvfs_result',
 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'curdir', 'defpath', 'devnull', 'dup', 'dup2
', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'ex
tsep', 'fdopen', 'fstat', 'fsync', 'getcwd', 'getcwdu', 'getenv', 'getpid', 'isatty', 'kill', 'linesep', 'listdir',
 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'popen2', 'po
pen3', 'popen4', 'putenv', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'spawnl', 'spawnle'
, 'spawnv', 'spawnve', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'sys',
 'system', 'tempnam', 'times', 'tmpfile', 'tmpnam', 'umask', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', '
walk', 'write']
>>> help(os.tmpfile)
Help on built-in function tmpfile in module nt:

tmpfile(...)
    tmpfile() -> file object

    Create a temporary file with no directory entries.

>>> print os.tmpfile()
<open file '<tmpfile>', mode 'w+b' at 0x02580CD8>

在python交互環境中,dir查找成員,help當作員的幫助文檔,然是直接在命令行測試這個api ---- 一鼓作氣!shell

下面談談就根據我對python粗淺的瞭解,所感覺到的其亮點:編程

  • 極佳的命令行交互體驗,實時測試:見上
  • 函數式編程的工具:filter + map + reduce: filter(lambda x: x > 10, range(1,20))
  • list comprehension:[x for x in range(1,10) if x % 2 == 0]
  • 多變量賦值:a, b = b, a
  • lambda: lambda x: x > 10
  • for和try的else支持 - 在其餘語言裏這個都得另外加flag實現,至關不便。
  • 內置的documentation strings支持: """ This is a documentation string """
  • generator-pull模式:須要時才產生: xrange()
  • chained comparation: a < b == c
  • 模塊機制:module - package
  • 標準的異常機制:try-except-finally; with statement
  • 內置的強大集合類:list, tuple, set, dict
  • 完善的標準庫
相關文章
相關標籤/搜索