0

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]])
2
  • 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. Commented May 21, 2013 at 1:56
  • Thanks for the advice! I've been far from python's news hehe. Commented May 21, 2013 at 1:58

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.