版本24和197间的区别 (跳过第173版)
于2006-06-10 18:51:16修订的的版本24
大小: 9089
编辑: czk
备注:
于2020-04-21 11:27:08修订的的版本197
大小: 12249
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 1: 行号 1:
[[TableOfContents]]
= Python语言 =

== Summary ==

Python is an interpreted programming language created by Guido van Rossum in 1990. Python is fully dynamically typed and uses automatic memory management; it is thus similar to Perl, Ruby, Scheme, Smalltalk, and Tcl. Python is developed as an open source project, managed by the non-profit Python Software Foundation, and is available for free from the project website. Python 2.4.3 was released on March 29, 2006.

||Paradigm编程范式:||Multi-paradigm多范式||
||Appeared in出现时间:||1990||
||Designed by设计者:||Guido van Rossum||
||Developer开发者:||Python Software Foundation||
||Latest release最后发布时间:||2.4.3 / March 29, 2006||
||Typing discipline类型规则:||Strong, dynamic ("duck")||
||Major implementations主要实现:||CPython, Jython, IronPython, PyPy||
||Influenced by受哪些语言影响:||ABC, Modula-3, Icon, C, Perl, Lisp, Smalltalk, Tcl||
||Influenced影响了哪些语言:||Ruby, Boo||
||OS操作系统:||Cross-platform||
||License许可证:||Python Software Foundation License||
||Website网站:||www.python.org||

== duck typing ==
In computer science, duck typing is a term for dynamic typing typical of some programming languages, such as Smalltalk or Visual FoxPro, where a variable's value itself determines what the variable can do. It also implies that an object is interchangeable with any other object that implements the same interface, regardless of whether the objects have a related inheritance hierarchy.

The term is a reference to the "duck test"—"If it walks like a duck and quacks like a duck, it must be a duck." One can also say that the duck typing method ducks the issue of typing variables.

Pythonic programming style that determines an object's type by inspection of its method or attribute signature rather than by explicit relationship to some type object ("If it looks like a duck and quacks like a duck, it must be a duck.") By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution. Duck-typing avoids tests using type() or isinstance(). Instead, it typically employs hasattr() tests or EAFP [Easier to Ask Forgiveness than Permission] programming.

The standard example of duck typing in Python is file-like classes. Classes can implement some or all of the methods of file and can be used where file would normally be used. For example, GzipFile implements a file-like object for accessing gzip-compressed data. cStringIO allows treating a Python string as a file. Sockets and files share many of the same methods as well. However, sockets lack the tell() method and cannot be used everywhere that GzipFile can be used. This shows the flexibility of duck typing: a file-like object can implement only methods it is able to, and consequently it can be only used in situations where it makes sense.

== Python设计哲学 ==

Python is a multi-paradigm language. This means that, rather than forcing coders to adopt one particular style of coding, it permits several. Object orientation, structured programming, functional programming, and aspect-oriented programming are all supported. Other paradigms can be supported through extensions, such as pyDBC and Contracts for Python which allow Design by Contract. Python is dynamically type-checked and uses garbage collection for memory management. An important feature of Python is dynamic name resolution, which binds method and variable names during program execution.

While offering choice in coding methodology, Python's designers reject exuberant syntax, such as in Perl, in favor of a sparser, less cluttered one. As with Perl, Python's developers expressly promote a particular "culture" or ideology based on what they want the language to be, favoring language forms they see as "beautiful", "explicit" and "simple". For the most part, Perl and Python users differ in their interpretation of these terms and how they are best implemented (see TIMTOWTDI and PythonPhilosophy).

Another important goal of the Python developers is making Python fun to use. This is reflected in the origin of the name (after the television series Monty Python's Flying Circus), in the common practice of using Monty Python references in example code, and in an occasionally playful approach to tutorials and reference materials. For example, the metasyntactic variables often used in Python literature are spam and eggs, instead of the traditional foo and bar.

Python is sometimes referred to as a "scripting language". In practice, it is used as a dynamic programming language for both application development and occasional scripting. Python has been used to develop many large software projects such as the Zope application server and the Mnet and BitTorrent file sharing systems. It is also extensively used by Google. [1]

Another important goal of the language is ease of extensibility. New built-in modules are easily written in C or C++. Python can also be used as an extension language for existing modules and applications that need a programmable interface.

Though the design of Python is somewhat hostile to functional programming (no tail call elimination or good support for anonymous closures) and the Lisp tradition, there are significant parallels between the philosophy of Python and that of minimalist Lisp-family languages such as Scheme. Many past Lisp programmers have found Python appealing for this reason.

== Pythonic ==

A few neologisms have come into common use within the Python community. One of the most common is "pythonic", which can have a wide range of meanings related to program style. To say that a piece of code is pythonic is to say that it uses Python idioms well; that it is natural or shows fluency in the language. Likewise, to say of an interface or language feature that it is pythonic is to say that it works well with Python idioms; that its use meshes well with the rest of the language.

In contrast, a mark of unpythonic code is that it attempts to "write C++ (or Lisp, or Perl) code in Python"—that is, provides a rough transcription rather than an idiomatic translation of forms from another language.

The prefix Py- can be used to show that something is related to Python. Examples of the use of this prefix in names of Python applications or libraries include Pygame, a binding of SDL to Python (commonly used to create games), PyUI, a GUI encoded entirely in Python, and PySol, a series of solitaire card games programmed in Python.

Users and admirers of Python—most especially those considered knowledgeable or experienced—are often referred to as Pythonists, Pythonistas, and Pythoneers

== Python的编码处理技术 ==
= Python =

<<TableOfContents>>

== Python入门教程 ==
 * [[Python介绍]]
 * Python教程:[[Python游戏开发基础]]
 * [[https://stackify.com/learn-python-tutorials/|30个不同的Python入门网站]]
 * [[https://developers.google.com/edu/python|Google's Python Class]]
 * [[https://blog.jetbrains.com/blog/2020/03/05/jetbrains-academy-python/|Learn Python with JetBrains Academy!]]
 * [[https://docs.python.org/3/tutorial/|Python Tutorial]]
 * [[https://diveintopython3.problemsolving.io/|Dive Into Python 3]] ([[https://woodpecker.org.cn/diveintopython3/|中文版]])
 * [[https://python.swaroopch.com/|A Byte of Python]] (中文版: [[https://bop.mol.uno/|简明Python教程]])
 * [[https://en.wikibooks.org/wiki/Python_Programming|Python Programming - Wikibooks]]
 * [[https://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3|Non-Programmers Tutorial For Python]]
 * [[https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-videos/|MIT 6.0001 Introduction to Computer Science and Programming in Python]]
 * [[http://openbookproject.net/thinkcs/python/english3e/|How to Think Like a Computer Scientist (Learning with Python 3)]]
 * [[http://xahlee.info/python/index.html|Python by Example]] by Xah Lee
 * [[http://www.alan-g.me.uk/l2p/|Learn to Program by Alan G]]
 * [[http://chinesepython.org/pythonfoundry/easytut.html|Python 非編程員教學]]

参考:
 * [[http://en.wikipedia.org/wiki/Python_%28programming_language%29|Wikipedia:Python_programming_language]] ([[http://zh.wikipedia.org/wiki/Python|Wikipedia上的Python编程语言简介]])
 * [[http://www.python.org/|Python Official Web]]
 * [[http://en.wikipedia.org/wiki/Python_syntax_and_semantics|Python语法]] from Wikipedia
 * [[http://www.linuxjournal.com/article/3882|Why_Python]] by Eric Raymond [[http://blog.sina.com.cn/s/blog_53982851010008b8.html|why python中文版]]
 * [[http://www.norvig.com/python-lisp.html|Python for Lisp Programmers]]

== Python解释器 ==
 * CPython http://python.org/
 * Stackless Python http://www.stackless.com/
   * [[StacklessPython]]
 * Ironpython (Python for .Net) https://ironpython.net/
 * Jython (Python for Java) https://www.jython.org/
 * [[https://www.pypy.org/|PyPy]]
   * [[https://speed.pypy.org/|PyPy Speed]]
 * [[http://hylang.org|Hylang]] A dialect of Lisp that's embedded in Python

== Python开发环境 ==
 * Mac上Python环境准备: https://sourabhbajaj.com/mac-setup/Python/
 * [[http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html|One Day of IDLE Toying]] (中文版: [[http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/IDLE-chinese.htm|摆弄一天IDLE]])
 * [[http://ftp.ntua.gr/mirror/python/idle/doc/idle2.html|Using IDLE]] by Daryl Harms
 * [[https://docs.python-guide.org/dev/virtualenvs/|Pipenv & Virtual Environments]]
 * [[https://hackernoon.com/reaching-python-development-nirvana-bb5692adf30c|Why you should use pyenv + Pipenv for your Python projects]]
 * [[https://pipenv.pypa.io/en/latest/advanced/|Advanced Usage of Pipenv]]



已经过时的教程(基于已经淘汰的Python 2):

 * 另类的Python教程: [[attachment:我的名字叫Python.pdf]] (太古老,已过时)
 * [[http://www.wag.caltech.edu/home/rpm/python_course/|Python Short Course]] by Richard P. Muller (太古老,已过时)
 * [[https://github.com/livewires/python/releases|The LiveWires Python Course]] 已过时
 * [[http://www.pentangle.net/python/handbook/|Handbook of the Physics Computing Course]] 已过时
 * [[http://www.math.pku.edu.cn/teachers/lidf/docs/Python/python-tutorial.html|Python入门]]

== Python语法 ==

 * [[https://docs.python-guide.org/writing/gotchas/|Python Gotchas]]
 * [[https://towardsdatascience.com/30-python-best-practices-tips-and-tricks-caefb9f8c5f5|30 Python Best Practices, Tips, And Tricks]]
 * [[https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/|Python yield 使用浅析]]
 * [[https://python-future.org/compatible_idioms.html|Cheat Sheet: Writing Python 2-3 compatible code]]
 * [[https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html|Type hints cheat sheet (Python 3)]]
 * [[https://docs.python.org/3/reference/index.html|Python Reference Manual]]
 * [[https://realpython.com/async-io-python/|Async IO in Python: A Complete Walkthrough]]
 * [[https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/|How the heck does async/await work in Python 3.5?]]
 * [[https://rhettinger.wordpress.com/2011/05/26/super-considered-super/|Python’s super() considered super! ]]
 * [[https://docs.python.org/3/library/asyncio-task.html|Coroutines and Tasks]]
 * [[https://www.python.org/dev/peps/pep-0492/|PEP 492 -- Coroutines with async and await syntax]]


== 代码风格 ==

 * [[https://www.python.org/doc/essays/styleguide/|Python Style Guide]]
 * https://www.python.org/dev/peps/pep-0008/
 * https://www.python.org/dev/peps/pep-0257/
 * [[https://docs.python-guide.org/writing/style/|Code Style -- The Hitchhiker's Guide to Python]]
 * [[https://www.jeffknupp.com/blog/2013/08/16/open-sourcing-a-python-project-the-right-way/|Open Sourcing a Python Project the Right Way]]

== 参考资料 ==

 * [[Why Python is More Productive than Java]]
 * awesome python https://github.com/vinta/awesome-python
 * [[https://woodpecker.org.cn/|WoodPecker中文社区]]
 * [[https://www.reddit.com/r/programming/comments/eroi/of_snakes_and_rubies_or_why_i_chose_python_over/|Of snakes and rubies; Or why I chose Python over Ruby]]
 * [[http://www-128.ibm.com/developerworks/cn/linux/theme/special/index.html#python|ibm上的python专题]]
 * [[http://www.hetland.org/python/instant-hacking.php|Instant Hacking]] and [[http://hetland.org/python/instant-python.php|Instant Python]]
 * [[http://wiki.python.org/moin/Intro_to_programming_with_Python_and_Tkinter|Intro to programming with Python in Tkinter(Video Lesson)]]

 * [[http://www.onlamp.com/pub/a/python/2002/10/17/biopython.html|Beginning Python for Bioinformatics]]
 * [[http://www.pasteur.fr/recherche/unites/sis/formation/python/|Python course in Bioinformatics]]
 * [[http://www.pasteur.fr/formation/infobio/python/|Introduction to Programming using Python]]
 * [[http://gvr.sourceforge.net/|The Guido van Robot]]
 * [[http://www.linuxjournal.com/article/3946|Python Programming for Beginners]]
 * [[http://www.rexx.com/~dkuhlman/python_101/python_101.html|Python 101 -- Introduction to Python]] [[http://www.rexx.com/~dkuhlman/python_201/python_201.html|Python 201 -- (Slightly) Advanced Python Topics]]
 * [[http://www.ifi.uio.no/in228/lecsplit/|Scripting for Computational Science]]
 * [[http://starship.python.net/crew/hinsen/|Python for Science]]
 * [[http://www.codeteacher.com/category.php?ID=8|Code Teacher]]
 * [[http://groups.google.com/group/comp.lang.python|Python讨论组comp.lang.python]]
 * [[http://pine.fm/LearnToProgram/|Learn to Program(ruby)]]
 * [[https://www.sitepoint.com/community/t/article-10-time-saving-tips-for-pythonists/197722|10 Time-saving Tips for Pythonists]]
 * [[https://docs.python.org/3.1/howto/advocacy.html|Python_Advocacy_HOWTO]] by A.M. Kuchling
 * Learning to Program: attachment: http://www.freenetpages.co.uk/hp/alan.gauld/
 * Learn To Program CD: [[attachment:LearnToProgramCD.zip]]
 * Tkinter Life Preserver: http://www.python.org/doc/life-preserver/
 * [[https://archive.org/details/SeanKellyRecoveryfromAddiction|Recovery from Addiction]]
 * [[https://www.zhidaow.com/post/python-modules-you-should-know|Python你必须知道的十个库]]
 * [[http://www.pythontip.com/blog/post/9771/|【外刊IT评论网】Python高效编程技巧]]
 * [[http://blog.sina.com.cn/s/blog_4e8581890102e29u.html|孩子们为什么要学Python编程?]]
 * [[https://pythonconquerstheuniverse.wordpress.com/2009/10/03/python-java-a-side-by-side-comparison/|Python & Java: A Side-by-Side Comparison]]
 * [[https://book.douban.com/subject/1241032/|Jython程序设计]]
 * [[https://lists.ubuntu.com/archives/ubuntu-devel/2020-February/040918.html|Python2 removal for Ubuntu focal]]
 * [[https://app.getpocket.com/read/2956522352|Python 2.7.18, the end of an era]]

== Python库 ==

=== 标准库 ===

 * [[http://docs.python.org/lib/lib.html|Python Library Reference]]

=== GUI Toolkit ===

 * wxpython http://www.wxpython.org/
 * [[PyGtk]]
 * [[https://kivy.org/|Kivy]] 基于OpenGL ES的移动端、桌面端GUI开发库
 * 使用PyQt编写QT图形界面程序 http://www.riverbankcomputing.co.uk/pyqt/
 * [[https://machinekoder.com/pyqt-vs-qt-for-python-pyside2-pyside/|PyQT vs PySide2]]

=== 网络 ===

 * [[https://github.com/MagicStack/uvloop|uvloop]]: asyncio + libuv + Cython,再配上httptools做HTTP parser,从[[https://magic.io/blog/uvloop-blazing-fast-python-networking/|benchmark]]看起来性能非常强劲。

=== Web 开发 ===

 * [[Django]]
 * [[https://wiki.python.org/moin/WebProgramming|WebProgramming - Python Wiki]]
 * Flask: https://flask.palletsprojects.com/en/1.1.x/
 * [[https://www.youtube.com/watch?v=DWODIO6aCUE|Better Web App]]

=== Windows ===
 * pywin32 http://sourceforge.net/projects/pywin32/
 * comtypes http://starship.python.net/crew/theller/comtypes/
 * python 与 COM [[http://oreilly.com/catalog/pythonwin32/chapter/ch12.html|Advanced Python and COM]]
 * 使用_winreg模块操纵windows注册表
 * 将python程序打包成windows下可以执行的exe文件:使用[[http://www.py2exe.org/|py2exe]]模块
 * 在.net平台上使用python:[[http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=ad7acff7-ab1e-4bcb-99c0-57ac5a3a9742|IronPython]]

=== Mac ===
 * [[http://undefined.org/python/py2app.html|Py2app]]

=== 多媒体 ===

 * 图像获取:linux平台下使用sane模块,Windows平台下使用twain模块
   * [[http://twainmodule.sourceforge.net/|TWAIN]]
 * 图像处理[[PythonImageLibrary中文手册]]
 * 通过ffmpeg处理视频、音频 https://github.com/kkroening/ffmpeg-python
 * PyDUB:在python中直接处理音频 https://github.com/jiaaro/pydub

=== 游戏 ===
 * pygame: http://www.pygame.org/
   * [[Pygame]]
 * panda3d: http://www.panda3d.org/
 * renpy: http://www.renpy.org/wiki/renpy/Home_Page
 * pyopengl http://pyopengl.sourceforge.net/

=== 字符串 ===

 * [[https://docs.python.org/3/howto/unicode.html|Unicode Howto]]
 * http://docs.python.org/lib/module-re.html
 * http://www.amk.ca/python/howto/regex/
 * Mastering Regular Expressions 2nd Edition, By Jeffrey E. F. Friedl

=== 数据处理 ===

 * [[https://numpy.org/|numpy]]
 * [[https://www.scipy.org/|scipy]]
 * [[https://radimrehurek.com/data_science_python/|Practical Data Science in Python]]

=== 其它 ===
 * python-ldap http://www.python-ldap.org/
   * [[PythonLdap]]
 * 读取excel:用xlrd包。把excel转成csv,使用csv库操作。
 * 加密:Python Cryptography Toolkit http://www.amk.ca/python/code/crypto
 * [[http://springpython.webfactional.com/|Spring Python]]: 为什么Python不需要像Spring这样的东西?

== Python的字符编码处理 ==
行号 91: 行号 223:
= Python库 =
 * 图像获取:linux平台下使用sane模块,Windows平台下使用twain模块

 * 图像处理["PythonImageLibrary中文手册"]

 * 使用_winreg模块操纵windows注册表

 * 读取excel:用xlrd包。把excel转成csv,使用csv库操作。

 * 将python程序打包成windows下可以执行的exe文件:使用[http://www.py2exe.org/ py2exe]模块

 * 加密:Python Cryptography Toolkit http://www.amk.ca/python/code/crypto
 * 在.net平台上使用python:[http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=ad7acff7-ab1e-4bcb-99c0-57ac5a3a9742 IronPython]

学习资料
 * [https://secure.wikimedia.org/wikipedia/zh/wiki/Python Wikipedia:Python]
 * [http://diveintopython.org/ Dive Into Python]
 * [http://www.woodpecker.org.cn/obp/diveintopython-zh-5.4/zh-cn/dist/html/toc/index.html dive into python最新中文版]
 * [http://www.python.org/ Python官方站点]
 * [http://python.cn/ Python中文社区]
 * [http://groups.google.com/group/comp.lang.python Python讨论组comp.lang.python]
 * [http://www.ibiblio.org/obp/thinkCSpy/ How to Think Like a Computer Scientist (Learning with Python)]
 * [http://pine.fm/LearnToProgram/ Learn to Program(ruby)]
 * [http://www-128.ibm.com/developerworks/cn/linux/theme/special/index.html#python]
 * [[Python代码片段]]

Python

1. Python入门教程

参考:

2. Python解释器

3. Python开发环境

已经过时的教程(基于已经淘汰的Python 2):

4. Python语法

5. 代码风格

6. 参考资料

7. Python库

7.1. 标准库

7.2. GUI Toolkit

7.3. 网络

  • uvloop: asyncio + libuv + Cython,再配上httptools做HTTP parser,从benchmark看起来性能非常强劲。

7.4. Web 开发

7.5. Windows

7.6. Mac

7.7. 多媒体

7.8. 游戏

7.9. 字符串

7.10. 数据处理

7.11. 其它

8. Python的字符编码处理

对于中文用户,特别需要关注Python的编码技术. 列举一些常用的技巧。

chr(i): 将一个0到255的整数转换为一个字符.
ord(c): 返回单个字符c的整数顺序值.普通字符返回[0,255]中的一个值,Unicode字符返回 [0,65535]中的一个值
unichr(i): 将一个0到65535的整数转换为一个Unicode字符
  • 代码中的编码设置,应该在代码最初两行内包含:

# -*- coding: UTF-8 -*-
  • 获得/设置系统的缺省编码

sys.getdefaultencoding()

sys.setdefaultencoding('utf-8')
  • 获得文件系统的文件名的编码

sys.getfilesystemencoding()
  • 获得当前终端的输入、输出编码

      sys.stdout.encoding

      sys.stdin.encoding
  • 编码转换(先转换为unicode,再转换为具体的编码),有两种方法:

      unicode('abc', 'mbcs').encode('utf-8')

      'abc'.decode('mbcs').encode('utf-8')

Python (2020-06-05 17:12:03由czk编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.