[SciPy-user] NumPy arrays of Python objects (it was Re: How to start with SciPy and NumPy)

David Cournapeau david@ar.media.kyoto-u.ac...
Mon Jan 26 05:45:18 CST 2009


Vicent wrote:
>
> (2) Just to be sure: An array can be assigned to a property of an
> object, can't it?

A numpy array is a 'full' python object, thus can be used in the same
cases as a python object. One thing to keep in mind though is that
arrays have some copy semantics which may surprise you:

# a is some sort of array
b = a

In that case, b is a new name for the content in a, and any change in b
will reflect in a. So if you have:

class A:
    def __init__(self, data):
        self.data = data

and data are modified outside A instances, the data inside the instances
will be changed as well. This is similar to python list, but there are
some differences as well.

> In fact, I know that some of my colleagues don't work with objects,
> but just with "structs" or "records" and functions that directly
> manage those "records". They work with C++ and Delphy, by the way.

Working with object or not is not generally the most relevant aspect of
good design - if you can do the same with a few functions and standard
python objects/containers, it is often simpler and better to use them. A
good example is simple file handling: if you compare the Java and the
python method, the python method is certainly more elegant, and don't
rely exclusively on objects as Java does.

cheers,

David


More information about the SciPy-user mailing list