Python programming languageCategory:Programming languages Category:Python programming language Python is an interpreted, interactive programming language created by Guido van Rossum, originally as a scripting language for Amoeba OS capable of making system calls. Python is often compared to Tcl, Perl, Scheme, Java and Ruby. Python is developed as an open source project, and is currently (as of July 2004) at version 2.3.4. The latest unstable version is 2.4a1.
numbers = [1, 2, 3, 4, 5]\n powers_of_two = [2**n for n in numbers]Because Python permits functions as arguments, it is also possible to express more subtle functional constructs, such as the continuation. LambdaPython'slambda keyword may misdirect some functional-programming fans. Python lambda blocks may contain only expressions, not statements. \nThus, they are not the most general way to return a function for use in higher-order functions. \nInstead, the usual practice is to define and return a function using a locally-scoped name, as in the following example of a simple curried function:
def add_and_print_maker(x):\n def temp(y):\n print "%d + %d = %d" % (x, y, x+y)\n return tempThe function can also be implemented with nested lambdas, as would be done in Scheme. \nTo do this requires working around the Python lambda's limitation, by defining a function to encapsulate the print statement:
def print_func(obj):\n print obj
add_and_print_maker = \\\n lambda(x): lambda(y): \\\n print_func("%d + %d = %d" % (x, y, x+y))
The resulting add_and_print_maker functions perform identically: given a number x they return a function which, when given a number y, will print a sentence of arithmetic. Although the first style may be more common, the second can be more clear to programmers with a functional-programming background.
Python's unique style for the binary boolean operators and and or create another unique functional feature. Using those two operators, any type of control flow can be implemented within lambda expressions [1]. \nThey are usually used for simpler purposes, however. \nSee the heading logical operators below.
GeneratorsIntroduced in Python 2.2 as optional feature and finalized in version 2.3, generators are Python's mechanism for lazy evaluation of a function that would otherwise return a long or computationally intensive list. \nThe uses of generators are similar to the uses of Scheme streams. One example from the python.org website:def generate_ints(N):\n for i in range(N):\n yield iYou can now use the generator generate_ints: for i in generate_ints(N):\n print iNote that the variable N should be defined before executing the second piece of code. The definition of a generator appears identical to that of a function, except the keyword yield is used in place of return. However, a generator is an object with persistent state, which can repeatedly enter and leave the same dynamic extent. A generator call can then be used in place of a list, or other structure whose elements will be iterated over. Whenever the for-loop in the example requires the next item, the generator is called, and yields the next item. Logical operatorsIn Python 2.2 and earlier, the expressions"", 0, None, (), [], {}, etc. are false, and everything else is true. When using binary boolean operators in Python, the syntax is to have the operator be in between the two statements in question. \nSo to see if the statements xx"> and 3 are true, one would write "x5 and 3". To evaluate this, the interpreter would first check if x returned true. If it didn't, it would return 0, but since it did, it goes on to the next statement. Next, it checks if 3 is true. Since 3 is true, 3 is returned. If three weren't true, 0 would be returned. If the order of all of this were reversed to 3 and x5, 1 would be returned because that's what x==5 evaluates to (because 1 is the default truth value). The or function works similarly. To find out if "2/3 or 5" is true, the interpreter first finds the truth value of 2/3. Since 2/3 evaluates to 0, as described above, it would return false. If it had returned true, then its value would be returned. Next, the interpreter looks at the second expression. Since, in this case, it returns true, 5 would be returned. \nIt is common in Python to write expressions such as print p or q to take advantage of this feature.
Later in Python 2.3 the keywords* True and False were added and, as a result, all of the binary comparison operators (2">, >, etc) return either True or False, while the rest of the aforementioned boolean operations (and, etc) still return the value that the last expression evaluated to. \nThus the expression "2 2" will return the value True and "2 == 2 and 5" still returns the integer 5.
*Under the hood, True and False are builtin objects of type bool which is a great example of the object oriented nature of Python.
Object-oriented programmingPython has inheritance, including multiple inheritance. It has limited support for private variables using name mangling. See the "Classes" section of the tutorial for details. \nMany Python users don't feel the need for private variables, though. \nThe slogan "We're all consenting adults here" is used to describe this attitude. \nSome consider information hiding to be unpythonic, in that it suggests that the class in question contains unaesthetic or ill-planned internals. From the tutorial: As is true for modules, classes in Python do not put an absolute barrier between definition and user, but rather rely on the politeness of the user not to "break into the definition." OOP doctrines such as the use of accessor methods to read data members are not enforced in Python. Just as Python offers functional-programming constructs but does not attempt to demand referential transparency (in contrast with Haskell), it offers (and extensively uses!) its object system but does not demand OOP behavior (in contrast with Java or Smalltalk). In version 2.2 of Python, "new-style" classes were introduced. With new-style classes, objects and types were unified, allowing the subclassing of types. \nEven new types entirely can be defined, complete with custom behavior for infix operators. This allows for many radical things to be done syntactically within Python, such as the ability to use C++-style input and output. \nA new multiple inheritance model was adopted with new-style classes, making a much more logical order of inheritance, adopted from Common Lisp. \nThe new method__getattribute__ was also defined for unconditional handling of attribute access.
Exception handlingPython supports (and extensively uses) exception handling as a means of testing for error conditions and other "exceptional" events in a program. Indeed, it is even possible to trap the exception caused by a syntax error! Exceptions permit more concise and reliable error checking than many other ways of reporting erroneous or exceptional events. Exceptions are thread-safe; they tend not to clutter up code in the way that testing for returned error codes does in C; and they can easily propagate up the calling stack when an error must be reported to a higher level of the program. Python style calls for the use of exceptions whenever an error condition might arise. Indeed, rather than testing for access to a file or resource before actually using it, it is conventional in Python to just go ahead and try to use it, catching the exception if access is rejected. Exceptions can also be used as a more general means of non-local transfer of control, even when an error is not at issue. For instance, the Mailman mailing list software, written in Python, uses exceptions to jump out of deeply-nested message-handling logic when a decision has been made to reject a message or hold it for moderator approval.Standard libraryPython has a large standard library, which makes it well suited to many tasks. This comes from a so-called "batteries included" philosophy for python modules. \nThe modules of the standard library can be augmented with custom modules written in either C or Python. The standard library is particularly well tailored to writing Internet-facing applications, with a large number of standard formats and protocols (such as MIME and HTTP) supported. Modules for creating graphical user interfaces, connecting to relational databases, and manipulating regular expressions are also included. The standard library is one of Python's greatest strengths. The bulk of it is cross-platform compatible, meaning that even heavily leveraged Python programs can often run on Unix, Windows, Macintosh, and other platforms without change. It is currently being debated whether or not third-party but open source Python modules such as wxPython or NumPy should be included in the standard library, in accordance with the batteries included philosophy.Other features\nLike Lisp, and unlike Perl, the Python interpreter also supports an interactive mode in which expressions can be entered from the terminal and results seen immediately. This is a boon for those learning the language and experienced developers alike: snippets of code can be tested in interactive mode before integrating them into a program proper. Python also includes a unit testing framework for creating exhaustive test suites. While static typing aficionados see this as a replacement for a static type-checking system, Python programmers largely do not share this view. Finally, Python supports continuations for manipulating control flow. Here is a list of articles with Python programs.NeologismsA few neologisms have come into common use within the Python community. \nOne 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, much as a prefixed J- denotes Java. Examples of the use of this prefix in names of Python applications or libraries include PyGame, a binding of SDL to Python, PyUI, a GUI encoded entirely in Python, PySol, a series of card games programmed in Python, and PyAlaMode, an IDE for Python created by Orbtech, a company specializing in Python.PlatformsAlthough Python was originally programmed for the Amoeba platform, that version is "dead" (ie. it hasn't been updated in a while). The most popular (and therefore best maintained) platforms Python runs on are Microsoft Windows, Linux, Mac OS X and Java. The Java version is a completely separate implementation which supports compilation. The Mac port is maintained by an external project called MacPython, and was included in Mac OS 10.3 "Panther". Other supported platforms include:\n* Mac Classic\n* SPARC Solaris\n* OS/2\n* Amiga\n* AROS\n* AS/400\n* BeOS\n* BSD\n* OS/390\n* z/OS\n* QNX\n* VMS\n* Psion\n* RISC OS (formerly Acorn)\n* VxWorks\n* PlayStation 2\n* Sharp Zaurus\n* Windows CE/Pocket PC\n* Palm OS Unfortunately, most of the third-party libraries for Python (and even some first-party ones) are only available on Windows, Linux, and Mac OS X.Miscellaneous
References\n*The Python Language Reference Manual by Guido van Rossum and Fred L. Drake, Jr. (ISBN 0-9541617-8-5)See also\n*Python Software Foundation - The PSF is a non-profit foundation devoted to Python.\n*Zope - Zope is an object-oriented web-application platform written in Python. Zope includes an application server with an integrated object-oriented database and a built-in web-based management interface.External links\n*The Python homepage\n*Python Wiki\n*Python Tutorials and References from Python official website.\n*Python at Wikibooks\n*Python Documentation\n*A Byte of Python, a beginner's book on Python\n*Dive into Python, a Python tutorial for programmers.\n*PythonPhotos.org, a public photo archive for the Python developer community.\n*python faqts\n*Python Wiki in Portuguese\n*How to Think Like a Computer Scientist Python versionPackages\n*PyGame Python game development\n*wxPython, a popular cross-platform GUI library for Python\n*PyGTK, a popular cross-platform GUI library based on GTK+\n*Twisted, a networking framework for Python \n\n\n\n\n\n\n \n\n\n\n\n\n\n Category:Free software |
||
"The difference between pornography and erotica is lighting." - Gloria Leonard |
