E.1. Built-in Functions

The following sections cover the functions built into Python. These functions are always available and do not require that a specific module be imported for their use.

__import__(mod)Imports the module represented by the string mod, especially useful for dynamically importing a list of modules:
myModules = ['sys','os','cgi','cgitb']
modules = map(__import__,myModules)
abs(n)Returns the absolute value of n.
basestring()Constructor for the built-in type that can be used to test whether an object is an instance of str or Unicode. This function can't be called or instantiated but is used like this:

if isinstance(obj, basestring):

bool([x])Returns True or False depending on the value of x. If x is a false statement or empty, returns False; otherwise, returns True.
callable(obj)Returns 1 if obj can be called; otherwise, returns 0.
chr(i)Returns a string of one character whose ASCII code is the integer i.
classmethod(func)Returns a class method for func in the following format:

class C:


def func(cls, arg1, arg2...):

cmp(a,b)Compares values a and b, returning a negative value if a < b, 0 if a = = b, and a positive value if a > b.
compile(string, filename, kind[,flags[, don't inherit]])Compiles string into a code object. Filename is the file containing the code. Kind denotes what kind of code to compile.
complex([real[,imag]])Returns a complex number with value real+imag*j.
delattr(obj,string)Removes the attribute of obj whose name is string

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.