12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

<strong>Python</strong> ranks the complexity of numeric types like so: integers are simpler than longintegers, which are simpler than floating-point numbers, which are simpler thancomplex numbers. So, when an integer is mixed with a floating point, as in thepreceding example, the integer is converted up to a floating-point value first, andfloating-point math yields the floating-point result. Similarly, any mixed-type expression,where one operand is a complex number, results in the other operand beingconverted up to a complex number, and the expression yields a complex result. Asyou’ll see later in this section, as of Release 2.2, <strong>Python</strong> also automatically convertsnormal integers to long integers whenever their values are too large to fit in a normalinteger.You can force the issue by calling built-in functions to convert types manually:>>> int(3.1415)3>>> float(3)3.0>>> long(4)4LHowever, you won’t usually need to do this: because <strong>Python</strong> automatically convertsup to the more complex type within an expression, the results are normally what youwant.Also, keep in mind that all these mixed-type conversions only apply when mixingnumeric types (e.g., an integer and a floating-point number) around an operator orcomparison. In general, <strong>Python</strong> does not convert across other type boundaries. Addinga string to an integer, for example, results in an error, unless you manuallyconvert one or the other; watch for an example when we meet strings in Chapter 7.Preview: Operator OverloadingAlthough we’re focusing on built-in numbers right now, keep in mind that all <strong>Python</strong>operators may be overloaded (i.e., implemented) by <strong>Python</strong> classes and C extensiontypes to work on objects you create. For instance, you’ll see later that objects codedwith classes may be added with + expressions, indexed with [i] expressions, and so on.Furthermore, <strong>Python</strong> itself automatically overloads some operators, such that theyperform different actions depending on the type of built-in objects being processed.For example, the + operator performs addition when applied to numbers, but performsconcatenation when applied to sequence objects such as strings and lists. Infact, + can mean anything at all when applied to objects you define with classes.As we saw in the prior chapter, this property is usually called polymorphism—a termindicating that the meaning of an operation depends on the type of objects beingoperated on. We’ll revisit this concept when we explore functions in Chapter 15because it becomes a much more obvious feature in that context.98 | Chapter 5: Numbers

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

Saved successfully!

Ooh no, something went wrong!