Scalar objects

Never worry about numbers. Help one person at a time, and always start with the person nearest you.

—Mother Teresa

A great many people think they are thinking when they are merely rearranging their prejudices.

— William James

One important new feature of NumPy is the addition of a new scalar object for each of the 21 different data types that an array can have. Do not confuse these scalar objects with the data-type objects. There is one data-type object. It contains a . type attribute which points to the Python type that each element of the array will be returned as1. The built-in data-types point have .type attributes that point to these scalar objects. Five (or six) of these new scalar objects are essentially equivalent to fundamental Python types and therefore inherit from them as well as from the generic array scalar type. The booL data type is very similar to the Python BooleanType but does not inherit from it because Python's BooleanType does not allow itself to be inherited from, and on the G-level the size of the actual booL data is not the same as a Python Boolean scalar. Table 6.1 shows which array scalars inherit from basic Python types.

xwith the exception of object data-types which return the underlying object and not a "scalar"

Table 6.1: Array scalar types that inherit from basic Python types. The intc array data type might also inherit from the IntType if it has the same number of bits as the int_ array data type on your platform.

array data type

Python type

int_

IntType

float_

FloatType

complex.

ComplexType

str_

StringType

Unicode.

UnicodeType

The array scalars have the same attributes and methods as arrays and live in a hierarchy of scalar types so they can be easily classified based on their type objects. However, because array scalars are immutable, and attributes change intrinsic properties of the object, the array scalar attributes are not settable.

Array scalars can be detected using the hierarchy of data types. For example, isinstance (val, generic) will return True if val is an array scalar object. Alternatively, what kind of array scalar is present can be determined using other members of the data type hierarchy. Thus, for example isinstance (val, complexfloating) will return True if val is a complex valued type, while isinstance (val, flexible) will return true if val is one of the flexible item-size array types (string, unicode, void).

WARNING

The booL type is not a subclass of the int_ type (the booL type is not even a number type). This is different than Python's default implementation of bool as a sub-class of int.

0 0

Post a comment