# JythonBook / sandbox / builtins.rst

## Constructor Functions

Constructor functions are used to create objects of a given type.

Note

In Python, the type is a constructor function; there's no difference at all in Python. So you can use the type function, which we will discuss momentarily, to look up the type of an object, then make instances of that same type.

First we will look at the constructor functions, which are more typically used for conversion. This is because there is generally a convenient literal syntax available, or in the case of bool, there are only two such constants, True and False.

Although there is a convenient literal for creating dict objects:

a_dict = { 'alpha' : 1, 'beta' : 2, 'gamma' : 3 }


It can be more convenient to create them using the dict function:

a_dict = dict(alpha=1, beta=2, gamma=3)


Of course in this latter case, the keys of the entries being created must be valid Python keywords.

Use as decorators: classmethod, staticmethod, property

slice is rarely used directly.

super type - 3 arg form compile

## Math Builtin Functions

Most math functions are defined in math (or cmath for complex math). These are functions that are builtin:

abs, cmp, divmod, pow, round

You may need to use named functions

## Functions on Iterables

The next group of builtin functions operate on iterables, which in Jython also includes all Java objects that implement the java.util.Iterator interface.

In particular,

The zip function creates a list of tuples by stepping through each iterable. One very common idiom is to use zip to create a dict where one iterable has the keys, and the other the values. This is often seen in working with CSV files (from a header row) or database cursors (from the description attribute). However, you might want to consider using collections.namedtuple instead:

XXX example code - read from CSV, zip together


The sorted function returns a sorted list. Use the optional key argument to specify a key function to control how it's sorted. So for example, this will sort the list by the length of the elements in it:

>>> sorted(['Massachusetts', 'Colorado', 'New York', 'California', 'Utah'], key=len)
['Utah', 'Colorado', 'New York', 'California', 'Massachusetts']


And this one will sort a list of Unicode strings without regard to it whether the characters are upper or lowercase:

>>> sorted(['apple', 'Cherry', 'banana'])
['Cherry', 'apple', 'banana']

>>> sorted(['apple', 'Cherry', 'banana'], key=str.upper)
['apple', 'banana', 'Cherry']


Although using a key function requires building a decorated version of the list to be sorted, in practice this uses substantially less overhead than calling a cmp function on every comparison. We recommend you take advantage of a keyed sort.

all and any will also short cut, if possible.

and sum(iterable[, start=0]) are functions that you will find frequent use for.

The max and min functions take a key function as an optional argument.

Although filter, map, and reduce are still useful, their use is largely superseded by using other functions, in conjunction with generator expressions. The range function is still useful for creating a list of a given sequence, but for portability eventualy to Python 3.x, using list(xrange()) instead is better.

• Generator expressions (or list comprehensions) are easier to use than filter.
• Most interesting but simple uses of reduce can be implemented through sum. And anything more complex should likely be written as a generator.

XXX some extra stuff here:

Returns True if all of the elements in the iterable are true, otherwise False and stop the iteration. (If the iterable is empty, this function returns True).

Returns True if any of the elements in the iterable are true, stopping the iteration. Otherwise returns False and stop the iteration. (If the iterable is empty, this function returns True).

Returns True if any of the

## Namespace Functions

namespace - __import__, delattr, dir, getattr, locals, globals, hasattr, reload, setattr, vars

getattr

compile, eval, exec Creating code objects.

evaluation - eval, execfile, predicates - callable, isinstance, issubclass hex, oct, id, hash, ord, repr len input, rawinput

Just refer to the documentation on these: deprecated functions - apply, buffer, coerce, intern ...

Operators