Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I receive an array (like numpy ones) into a function? Let's say the array a = [[2],[3]] and the function f. Tuples works like this:

def f((a ,b)):
    print a * b

f((2,3))

But how is it with arrays?

def f(#here):
    print a*b

f([[2],[3]])
share|improve this question
1  
As a side note, the form of the first function is a SyntaxError in python3.x. I would advise moving away from using that construct. –  mgilson May 21 '13 at 1:56
 
Thanks for the advice! I've been far from python's news hehe. –  Alejandro Sazo May 21 '13 at 1:58
add comment

1 Answer

up vote 1 down vote accepted

Tuple unpacking in function arguments has been removed from Python 3, so I suggest you stop using it.

Lists can be unpacked just like tuples:

def f(arg):
    [[a], [b]] = arg

    return a * b

Or as a tuple:

((x,), (y,)) = a

But I would just use indexes:

return arg[0][0] * arg[1][0]
share|improve this answer
 
Thanks for the unpacking advice. @mgilson wrote it below the question but both were helpful. I use a and b because the goal is to be clear in the code (a and b has their meaning in a context). –  Alejandro Sazo May 21 '13 at 2:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.