15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In addition, because of the unification of types and classes in <strong>Python</strong> 2.2, all of these built-in functions<br />

were converted into factory functions. Factory functions, introduced in Chapter 4, just means that these<br />

objects are now classes, and when you "call" them, you are just creating an instance of that class.<br />

They will still behave in a similar way to the new <strong>Python</strong> user so it is probably something you do not<br />

have to worry about.<br />

The following are some examples of using these functions:<br />

>>> int(4.25555)<br />

4<br />

>>> long(42)<br />

42L<br />

>>> float(4)<br />

4.0<br />

>>> complex(4)<br />

(4+0j)<br />

>>><br />

>>> complex(2.4, -8)<br />

(2.4-8j)<br />

>>><br />

>>> complex(2.3e-10, 45.3e4)<br />

(2.3e-10+453000j)<br />

Table 5.5 summarizes the numeric type factory functions.<br />

Table 5.5. Numeric Type Factory Functions [a]<br />

Class (Factory Function) Operation<br />

[b] Returns the Boolean value of obj, e.g., the value of<br />

bool(obj)<br />

executing obj.__nonzero__()<br />

int(obj, base=10) Returns integer representation of string or number obj;<br />

similar to string.atoi(); optional base argument<br />

introduced in 1.6<br />

long(obj, base=10) Returns long representation of string or number obj;<br />

similar to string.atol(); optional base argument<br />

introduced in 1.6<br />

float(obj) Returns floating point representation of string or number<br />

obj; similar to string.atof()<br />

complex(str) or complex(real, imag=0.0) Returns complex number representation of str, or builds<br />

one given real (and perhaps imaginary) component(s)<br />

[a] Prior to <strong>Python</strong> 2.3, these were all built-in functions.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!