1

As you read through this question, you will see I am new to Python and NumPy, so please excuse any incorrect terminology.

I am currently working with two NumPy arrays, let's call them x and y.

x = numpy.array(0)
y = numpy.array(0)

I am operating on these arrays with functions that have these arrays as input and output.

x, y = function1(x, y)
x, y = function2(x, y)
...

This seems inelegant because if I could just make them parts of the same object, then I would only have a single input and output to these functions. I just want to make my code simpler, if possible.

x = function1(x)
x = function2(x)
...

As it turns out, y describes the data in x (not in this toy example, but with my actual arrays) so I figured I would set y as a property of x. My native language is MATLAB and I figured I could use syntax similar to creating struct hierarchies like so.

x.y = y

This doesn't work and, unfortunately, I don't know enough Python terminology to know what to look for online to find a solution to my problem.

Please let me know if you have any suggestions and/or if I can provide any clarification on this problem.

Gyan Veda
  • 6,309
  • 11
  • 41
  • 66
  • Why do you show us a minimal *complete* example of what you are doing currently, and say a few words about *why* you find the current approach unsatisfactory? – NPE Feb 14 '14 at 21:26
  • I don't understand your question, NPE. You want an incomplete example? I can make the data more realistic, but what you see above will repliacte the error I get. – Gyan Veda Feb 14 '14 at 21:27
  • 1
    see [this](http://stackoverflow.com/questions/1529002/cant-set-attributes-of-object-class) I don't think it can be done without subclassing the numpy arrays. – M4rtini Feb 14 '14 at 21:34
  • 1
    I'm not sure why you would do it like this, the example isn't clear *why* you want to add y as an attribute of x. This is allowed in pandas datastructures. – Andy Hayden Feb 14 '14 at 21:34
  • Your motivation is entirely unclear to me. I was hoping a minimal yet meaningful example would shed some light on your thinking, enabling us to better help you. – NPE Feb 14 '14 at 21:38
  • Thanks, M4rtini. I'll just keep things as they are not bother with trying to add one as the attribute of the other. Andy, I'm doing this so I don't have to have 2 inputs and 2 outputs to each of the many functions that are doing things to these two arrays. I thought I could make them a single object and then just have this single objet be the input and output to these functions. Just for simpler code. That's the main motivation. – Gyan Veda Feb 14 '14 at 21:40
  • Gotcha, NPE. There's a typo in your question and it seemed like you were asking for a less complete example. The motivation was mentioned in the middle of the question, but I made it more explicit now so I hope it's clearer. Thanks for commenting. – Gyan Veda Feb 14 '14 at 21:48

2 Answers2

1

I assume that the error you're getting is along the lines of 'array' object has no attribute 'y'.

In this situation, if you really just want to bundle these things together, I'd recommend the use of a tuple. Here's some examples of how they work for your use case:

def myFunc1(x, y): 
    # do stuff with x and y
    return ??? # x or y or... both, somehow? 

def myFunc2(xy_bundle): # where xy_bundle is x and y packed into a tuple! 
    x, y = xy_bundle    # or: x = xy_bundle[0]; y = xy_bundle[1];
    # do stuff with x and y
    return (x, y)       # syntax for creating a tuple inline

You can use tup[x] notation to access individual members of a tuple, just like a list or an array, or you can implicitly unpack them, as in myFunc2. Tuples are a pretty standard tool for moving multiple things through functions, though if you find yourself using them a lot, you might have a case for a custom class or something.

As has been said in the comments, it's not clear what your motivation for wanting to make an array a property of another array, but it seems like a bad idea to me. If you have arrays that are dependent on one another, you could definitely subclass pandas' array and add a property to it. Seems overly complex, though.

Edit: calling these things! Your code probably looks like:

new_x = do_thing_with_array(x)
new_y = do_thing_with_array(y)

This would generally change to:

arrs = (x, y)
new_x, new_y = do_thing_with_arrays(arrs)

Hope that's what you were looking for.

a p
  • 3,098
  • 2
  • 24
  • 46
  • Thanks, a p! I'll have to look into custom classes with already-existing ones like NumPy arrays. Otherwise, I'll try out the bundles you mention. Just curious, why does this seem like a bad idea to you? Trying to understand how to write better Python code. My main motivation is having more elegant code by having fewer variables floating around my workspace and passing less of them to my functions. – Gyan Veda Feb 14 '14 at 21:42
1

You could create a simple class holding both arrays like this.

class Xy(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y 

import numpy as np 

x = np.array(range(10))
y = np.array(range(10))

xy = Xy(x,y)

def square(xy):
    x = xy.x 
    y = xy.y

    x **= 2
    y **= 2

    return Xy(x,y)
M4rtini
  • 13,186
  • 4
  • 35
  • 42