A Question Answer System for Python
Yes.
The pdb module is a simple but adequate console-mode debugger for Python. It is part of the standard Python library, and is documented in the Library Reference Manual
. You can also write your own debugger by using the code for pdb as an example.
The IDLE interactive development environment, which is part of the standard Python distribution (normally available as Tools/scripts/idle), includes a graphical debugger.
PythonWin is a Python IDE that includes a GUI debugger based on pdb. The Pythonwin debugger colors breakpoints and has quite a few cool features such as debugging non-Pythonwin programs. Pythonwin is available as part of the Python for Windows Extensions project and as a part of the ActivePython distribution (see https://www.activestate.com/activepython).
Boa Constructor is an IDE and GUI builder that uses wxWidgets. It offers visual frame creation and manipulation, an object inspector, many views on the source like object browsers, inheritance hierarchies, doc string generated html documentation, an advanced debugger, integrated help, and Zope support.
Eric is an IDE built on PyQt and the Scintilla editing component.
Pydb is a version of the standard Python debugger pdb, modified for use with DDD (Data Display Debugger), a popular graphical debugger front end. Pydb can be found at http://bashdb.sourceforge.net/pydb/ and DDD can be found at https://www.gnu.org/software/ddd.
There are a number of commercial Python IDEs that include graphical debuggers. They include:
For Unix variants: The standard Python source distribution comes with a curses module in the Modules subdirectory, though it's not compiled by default. (Note that this is not available in the Windows distribution â there is no curses module for Windows.)
The curses
module supports basic curses features as well as many additional functions from ncurses and SYSV curses such as colour, alternative character set support, pads, and mouse support. This means the module isn't compatible with operating systems that only have BSD curses, but there don't seem to be any currently maintained OSes that fall into this category.
For Windows: use the consolelib module.
Not as such.
For simple input parsing, the easiest approach is usually to split the line into whitespace-delimited words using the split()
method of string objects and then convert decimal strings to numeric values using int()
or float()
. split()
supports an optional "sep" parameter which is useful if the line uses something other than whitespace as a separator.
For more complicated input parsing, regular expressions are more powerful than C's sscanf()
and better suited for the task.
The atexit
module provides a register function that is similar to C's onexit()
.
Yes, there is. The syntax is as follows:
[on_true] if [expression] else [on_false] x, y = 50, 25 small = x if x < y else y
Before this syntax was introduced in Python 2.5, a common idiom was to use logical operators:
[expression] and [on_true] or [on_false]
However, this idiom is unsafe, as it can give wrong results when on_true has a false boolean value. Therefore, it is always better to use the ... if ... else ...
form.
If you can't find a source file for a module it may be a built-in or dynamically loaded module implemented in C, C++ or other compiled language. In this case you may not have the source file or it may be something like mathmodule.c
, somewhere in a C source directory (not on the Python Path).
There are (at least) three kinds of modules in Python:
modules written in Python (.py);
modules written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc);
modules written in C and linked with the interpreter; to get a list of these, type:
import sys print(sys.builtin_module_names)
The latest Python source distribution is always available from python.org, at https://www.python.org/downloads/. The latest development sources can be obtained via anonymous Mercurial access at https://hg.python.org/cpython.
The source distribution is a gzipped tar file containing the complete C source, Sphinx-formatted documentation, Python library modules, example programs, and several useful pieces of freely distributable software. The source will compile and run out of the box on most UNIX platforms.
Consult the Getting Started section of the Python Developer's Guide for more information on getting the source code and compiling it.
See the chapters titled Internet Protocols and Support and Internet Data Handling in the Library Reference Manual. Python has many modules that will help you build server-side and client-side web systems.
A summary of available frameworks is maintained by Paul Boddie at https://wiki.python.org/moin/WebProgramming.
Cameron Laird maintains a useful set of pages about Python web technologies at http://phaseit.net/claird/comp.lang.python/web_python.
The FAQ does not recommend using tabs, and the Python style guide, PEP 8, recommends 4 spaces for distributed Python code; this is also the Emacs python-mode default.
Under any editor, mixing tabs and spaces is a bad idea. MSVC is no different in this respect, and is easily configured to use spaces: Take
, and for file type "Default" set "Tab size" and "Indent size" to 4, and select the "Insert spaces" radio button.If you suspect mixed tabs and spaces are causing problems in leading whitespace, run Python with the -t
switch or run Tools/Scripts/tabnanny.py
to check a directory tree in batch mode.
A class is the particular object type created by executing a class statement. Class objects are used as templates to create instance objects, which embody both the data (attributes) and code (methods) specific to a datatype.
A class can be based on one or more other classes, called its base class(es). It then inherits the attributes and methods of its base classes. This allows an object model to be successively refined by inheritance. You might have a generic Mailbox
class that provides basic accessor methods for a mailbox, and subclasses such as MboxMailbox
, MaildirMailbox
, OutlookMailbox
that handle various specific mailbox formats.
Python sequences are indexed with positive numbers and negative numbers. For positive numbers 0 is the first index 1 is the second index and so forth. For negative indices -1 is the last index and -2 is the penultimate (next to last) index and so forth. Think of seq[-n]
as the same as seq[len(seq)-n]
.
Using negative indices can be very convenient. For example S[:-1]
is all of the string except for its last character, which is useful for removing the trailing newline from a string.
In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function's body, it's assumed to be a local unless explicitly declared as global.
Though a bit surprising at first, a moment's consideration explains this. On one hand, requiring global
for assigned variables provides a bar against unintended side-effects. On the other hand, if global
was required for all global references, you'd be using global
all the time. You'd have to declare as global every reference to a built-in function or to a component of an imported module. This clutter would defeat the usefulness of the global
declaration for identifying side-effects.
A method is a function on some object x
that you normally call as x.name(arguments...)
. Methods are defined as functions inside the class definition:
class C: def meth(self, arg): return arg * 2 + self.attribute
Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. Python combines remarkable power with very clear syntax. It has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++. It is also usable as an extension language for applications that need a programmable interface. Finally, Python is portable: it runs on many Unix variants, on the Mac, and on Windows 2000 and later.
To find out more, start with The Python Tutorial. The Beginner's Guide to Python links to other introductory tutorials and resources for learning Python.
The Python Software Foundation is an independent non-profit organization that holds the copyright on Python versions 2.1 and newer. The PSF's mission is to advance open source technology related to the Python programming language and to publicize the use of Python. The PSF's home page is at https://www.python.org/psf/.
Donations to the PSF are tax-exempt in the US. If you use Python and find it helpful, please contribute via the PSF donation page.
Lists and tuples, while similar in many respects, are generally used in fundamentally different ways. Tuples can be thought of as being similar to Pascal records or C structs; they're small collections of related data which may be of different types which are operated on as a group. For example, a Cartesian coordinate is appropriately represented as a tuple of two or three numbers.
Lists, on the other hand, are more like arrays in other languages. They tend to hold a varying number of objects all of which have the same type and which are operated on one-by-one. For example, os.listdir(''.'')
returns a list of strings representing the files in the current directory. Functions which operate on this output would generally not break if you added another file or two to the directory.
Tuples are immutable, meaning that once a tuple has been created, you can't replace any of its elements with a new value. Lists are mutable, meaning that you can always change a list's elements. Only immutable elements can be used as dictionary keys, and hence only tuples and not lists can be used as keys.
Python is a high-level general-purpose programming language that can be applied to many different classes of problems.
The language comes with a large standard library that covers areas such as string processing (regular expressions, Unicode, calculating differences between files), Internet protocols (HTTP, FTP, SMTP, XML-RPC, POP, IMAP, CGI programming), software engineering (unit testing, logging, profiling, parsing Python code), and operating system interfaces (system calls, filesystems, TCP/IP sockets). Look at the table of contents for The Python Standard Library to get an idea of what's available. A wide variety of third-party extensions are also available. Consult the Python Package Index to find packages of interest to you.
Python is a programming language. It's used for many different applications. It's used in some high schools and colleges as an introductory programming language because Python is easy to learn, but it's also used by professional software developers at places such as Google, NASA, and Lucasfilm Ltd.
If you wish to learn more about Python, start with the Beginner's Guide to Python.
Very stable. New, stable releases have been coming out roughly every 6 to 18 months since 1991, and this seems likely to continue. Currently there are usually around 18 months between major releases.
The developers issue "bugfix" releases of older versions, so the stability of existing releases gradually improves. Bugfix releases, indicated by a third component of the version number (e.g. 2.5.3, 2.6.2), are managed for stability; only fixes for known problems are included in a bugfix release, and it's guaranteed that interfaces will remain the same throughout a series of bugfix releases.
The latest stable releases can always be found on the Python download page. There are two recommended production-ready versions at this point in time, because at the moment there are two branches of stable releases: 2.x and 3.x. Python 3.x may be less useful than 2.x, since currently there is more third party software available for Python 2 than for Python 3. Python 2 code will generally not run unchanged in Python 3.
A try/except block is extremely efficient if no exceptions are raised. Actually catching an exception is expensive. In versions of Python prior to 2.0 it was common to use this idiom:
try: value = mydict[key] except KeyError: mydict[key] = getvalue(key) value = mydict[key]
This only made sense when you expected the dict to have the key almost all the time. If that wasn't the case, you coded it like this:
if key in mydict: value = mydict[key] else: value = mydict[key] = getvalue(key)
For this specific case, you could also use value = dict.setdefault(key, getvalue(key))
, but only if the getvalue()
call is cheap enough because it is evaluated in all cases.
Users are often surprised by results like this:
>>> 1.2 - 1.0 0.19999999999999996
and think it is a bug in Python. It's not. This has little to do with Python, and much more to do with how the underlying platform handles floating-point numbers.
The float
type in CPython uses a C double
for storage. A float
object's value is stored in binary floating-point with a fixed precision (typically 53 bits) and Python uses C operations, which in turn rely on the hardware implementation in the processor, to perform floating-point operations. This means that as far as floating-point operations are concerned, Python behaves like many popular languages including C and Java.
Many numbers that can be written easily in decimal notation cannot be expressed exactly in binary floating-point. For example, after:
>>> x = 1.2
the value stored for x
is a (very good) approximation to the decimal value 1.2
, but is not exactly equal to it. On a typical machine, the actual stored value is:
1.0011001100110011001100110011001100110011001100110011 (binary)
which is exactly:
1.1999999999999999555910790149937383830547332763671875 (decimal)
The typical precision of 53 bits provides Python floats with 15-16 decimal digits of accuracy.
For a fuller explanation, please see the floating point arithmetic chapter in the Python tutorial.
There are several advantages.
One is performance: knowing that a string is immutable means we can allocate space for it at creation time, and the storage requirements are fixed and unchanging. This is also one of the reasons for the distinction between tuples and lists.
Another advantage is that strings in Python are considered as "elemental" as numbers. No amount of activity will change the value 8 to anything else, and in Python, no amount of activity will change the string "eight" to anything else.
You can do this easily enough with a sequence of if... elif... elif... else
. There have been some proposals for switch statement syntax, but there is no consensus (yet) on whether and how to do range tests. See PEP 275 for complete details and the current status.
For cases where you need to choose from a very large number of possibilities, you can create a dictionary mapping case values to functions to call. For example:
def function_1(...): ... functions = {''a'': function_1, ''b'': function_2, ''c'': self.method_1, ...} func = functions[value] func()
For calling methods on objects, you can simplify yet further by using the getattr()
built-in to retrieve methods with a particular name:
def visit_a(self, ...): ... ... def dispatch(self, value): method_name = ''visit_'' + str(value) method = getattr(self, method_name) method()
It's suggested that you use a prefix for the method names, such as visit_
in this example. Without such a prefix, if values are coming from an untrusted source, an attacker would be able to call any method on your object.
Parameters are defined by the names that appear in a function definition, whereas arguments are the values actually passed to a function when calling it. Parameters define what types of arguments a function can accept. For example, given the function definition:
def func(foo, bar=None, **kwargs): pass
foo, bar and kwargs are parameters of func
. However, when calling func
, for example:
func(42, bar=314, extra=somevar)
the values 42
, 314
, and somevar
are arguments.
Yes, .pyd files are dll's, but there are a few differences. If you have a DLL named foo.pyd
, then it must have a function PyInit_foo()
. You can then write Python "import foo", and Python will search for foo.pyd (as well as foo.py, foo.pyc) and if it finds it, will attempt to call PyInit_foo()
to initialize it. You do not link your .exe with foo.lib, as that would cause Windows to require the DLL to be present.
Note that the search path for foo.pyd is PYTHONPATH, not the same as the path that Windows uses to search for foo.dll. Also, foo.pyd need not be present to run your program, whereas if you linked your program with a dll, the dll is required. Of course, foo.pyd is required if you want to say import foo
. In a DLL, linkage is declared in the source code with __declspec(dllexport)
. In a .pyd, linkage is defined in a list of available functions.
Delegation is an object oriented technique (also called a design pattern). Let's say you have an object x
and want to change the behaviour of just one of its methods. You can create a new class that provides a new implementation of the method you're interested in changing and delegates all other methods to the corresponding method of x
.
Python programmers can easily implement delegation. For example, the following class implements a class that behaves like a file but converts all written data to uppercase:
class UpperOut: def __init__(self, outfile): self._outfile = outfile def write(self, s): self._outfile.write(s.upper()) def __getattr__(self, name): return getattr(self._outfile, name)
Here the UpperOut
class redefines the write()
method to convert the argument string to uppercase before calling the underlying self.__outfile.write()
method. All other methods are delegated to the underlying self.__outfile
object. The delegation is accomplished via the __getattr__
method; consult the language reference for more information about controlling attribute access.
Note that for more general cases delegation can get trickier. When attributes must be set as well as retrieved, the class must define a __setattr__()
method too, and it must do so carefully. The basic implementation of __setattr__()
is roughly equivalent to the following:
class X: ... def __setattr__(self, name, value): self.__dict__[name] = value ...
Most __setattr__()
implementations must modify self.__dict__
to store local state for self without causing an infinite recursion.
Comma is not an operator in Python. Consider this session:
>>> "a" in "b", "a" (False, ''a'')
Since the comma is not an operator, but a separator between expressions the above is evaluated as if you had entered:
("a" in "b"), "a"
not:
"a" in ("b", "a")
The same is true of the various assignment operators (=
, +=
etc). They are not truly operators but syntactic delimiters in assignment statements.
You can use exceptions to provide a "structured goto" that even works across function calls. Many feel that exceptions can conveniently emulate all reasonable uses of the "go" or "goto" constructs of C, Fortran, and other languages. For example:
class label(Exception): pass # declare a label try: ... if condition: raise label() # goto label ... except label: # where to goto pass ...
This doesn't allow you to jump into the middle of a loop, but that's usually considered an abuse of goto anyway. Use sparingly.
You could define an alias for the base class, assign the real base class to it before your class definition, and use the alias throughout your class. Then all you have to change is the value assigned to the alias. Incidentally, this trick is also handy if you want to decide dynamically (e.g. depending on availability of resources) which base class to use. Example:
BaseAlias = <real base class> class Derived(BaseAlias): def meth(self): BaseAlias.meth(self) ...
Practical answer:
Cython and Pyrex compile a modified version of Python with optional annotations into C extensions. Weave makes it easy to intermingle Python and C code in various ways to increase performance. Nuitka is an up-and-coming compiler of Python into C++ code, aiming to support the full Python language.
Theoretical answer:
Not trivially. Python's high level data types, dynamic typing of objects and run-time invocation of the interpreter (using eval()
or exec()
) together mean that a naïvely "compiled" Python program would probably consist mostly of calls into the Python run-time system, even for seemingly simple operations like x+1
.
Several projects described in the Python newsgroup or at past Python conferences have shown that this approach is feasible, although the speedups reached so far are only modest (e.g. 2x). Jython uses the same strategy for compiling to Java bytecode. (Jim Hugunin has demonstrated that in combination with whole-program analysis, speedups of 1000x are feasible for small demo programs. See the proceedings from the 1997 Python conference for more information.)
Generally speaking, it can't, because objects don't really have names. Essentially, assignment always binds a name to a value; The same is true of def
and class
statements, but in that case the value is a callable. Consider the following code:
>>> class A: ... pass ... >>> B = A >>> a = B() >>> b = a >>> print(b) <__main__.A object at 0x16D07CC> >>> print(a) <__main__.A object at 0x16D07CC>
Arguably the class has a name: even though it is bound to two names and invoked through the name B the created instance is still reported as an instance of class A. However, it is impossible to say whether the instance's name is a or b, since both names are bound to the same value.
Generally speaking it should not be necessary for your code to "know the names" of particular values. Unless you are deliberately writing introspective programs, this is usually an indication that a change of approach might be beneficial.
In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer to this question:
The same way as you get the name of that cat you found on your porch: the cat (object) itself cannot tell you its name, and it doesn't really care â so the only way to find out what it's called is to ask all your neighbours (namespaces) if it's their cat (object)...
....and don't be surprised if you'll find that it's known by many names, or no name at all!
There is a newsgroup, comp.lang.python, and a mailing list, python-list. The newsgroup and mailing list are gatewayed into each other â if you can read news it's unnecessary to subscribe to the mailing list. comp.lang.python is high-traffic, receiving hundreds of postings every day, and Usenet readers are often more able to cope with this volume.
Announcements of new software releases and events can be found in comp.lang.python.announce, a low-traffic moderated list that receives about five postings per day. It's available as the python-announce mailing list.
More info about other mailing lists and newsgroups can be found at https://www.python.org/community/lists/.
You can use S.rstrip(" ")
to remove all occurrences of any line terminator from the end of the string S
without removing other trailing whitespace. If the string S
represents more than one line, with several empty lines at the end, the line terminators for all the blank lines will be removed:
>>> lines = ("line 1 " ... " " ... " ") >>> lines.rstrip(" ") ''line 1 ''
Since this is typically only desired when reading text one line at a time, using S.rstrip()
this way works well.
Yes.
PyChecker is a static analysis tool that finds bugs in Python source code and warns about code complexity and style. You can get PyChecker from http://pychecker.sourceforge.net/.
Pylint is another tool that checks if a module satisfies a coding standard, and also makes it possible to write plug-ins to add a custom feature. In addition to the bug checking that PyChecker performs, Pylint offers some additional features such as checking line length, whether variable names are well-formed according to your coding standard, whether declared interfaces are fully implemented, and more. https://docs.pylint.org/ provides a full list of Pylint's features.
There are numerous tutorials and books available. The standard documentation includes The Python Tutorial.
Consult the Beginner's Guide to find information for beginning Python programmers, including lists of tutorials.
You can do anything you want with the source, as long as you leave the copyrights in and display those copyrights in any documentation about Python that you produce. If you honor the copyright rules, it's OK to use Python for commercial use, to sell copies of Python in source or binary form (modified or unmodified), or to sell products that incorporate Python in some form. We would still like to know about all commercial use of Python, of course.
See the PSF license page to find further explanations and a link to the full text of the license.
The Python logo is trademarked, and in certain cases permission is required to use it. Consult the Trademark Usage Policy for more information.
Yes. The coding style required for standard library modules is documented as PEP 8.
It's probably best to cite your favorite book about Python.
The very first article about Python was written in 1991 and is now quite outdated.
Guido van Rossum and Jelke de Boer, "Interactively Testing Remote Servers Using the Python Programming Language", CWI Quarterly, Volume 4, Issue 4 (December 1991), Amsterdam, pp 283-303.
Yes, there are many, and more are being published. See the python.org wiki at https://wiki.python.org/moin/PythonBooks for a list.
You can also search online bookstores for "Python" and filter out the Monty Python references; or perhaps search for "Python" and "language".
There are a number of alternatives to writing your own C extensions, depending on what you're trying to do.
Cython and its relative Pyrex are compilers that accept a slightly modified form of Python and generate the corresponding C code. Cython and Pyrex make it possible to write an extension without having to learn Python's C API.
If you need to interface to some C or C++ library for which no Python extension currently exists, you can try wrapping the library's data types and functions with a tool such as SWIG. SIP, CXX Boost, or Weave are also alternatives for wrapping C++ libraries.
Yes.
Interfaces to disk-based hashes such as DBM
and GDBM
are also included with standard Python. There is also the sqlite3
module, which provides a lightweight disk-based relational database.
Support for most relational databases is available. See the DatabaseProgramming wiki page for details.
Remember that arguments are passed by assignment in Python. Since assignment just creates references to objects, there's no alias between an argument name in the caller and callee, and so no call-by-reference per se. You can achieve the desired effect in a number of ways.
By returning a tuple of the results:
def func2(a, b): a = ''new-value'' # a and b are local names b = b + 1 # assigned to new objects return a, b # return new values x, y = ''old-value'', 99 x, y = func2(x, y) print(x, y) # output: new-value 100
This is almost always the clearest solution.
By using global variables. This isn't thread-safe, and is not recommended.
By passing a mutable (changeable in-place) object:
def func1(a): a[0] = ''new-value'' # ''a'' references a mutable list a[1] = a[1] + 1 # changes a shared object args = [''old-value'', 99] func1(args) print(args[0], args[1]) # output: new-value 100
By passing in a dictionary that gets mutated:
def func3(args): args[''a''] = ''new-value'' # args is a mutable dictionary args[''b''] = args[''b''] + 1 # change it in-place args = {''a'': ''old-value'', ''b'': 99} func3(args) print(args[''a''], args[''b''])
Or bundle up values in a class instance:
class callByRef: def __init__(self, **args): for (key, value) in args.items(): setattr(self, key, value) def func4(args): args.a = ''new-value'' # args is a mutable callByRef args.b = args.b + 1 # change object in-place args = callByRef(a=''old-value'', b=99) func4(args) print(args.a, args.b)
There's almost never a good reason to get this complicated.
Your best choice is to return a tuple containing the multiple results.