0

I tried researching on a way to return multiple values using single return variable. What i want to achieve is, i want the code below to act as if it is returning multiple value instead of returning a tuple

def my_data:
    test_data = 1, 2, 3, 4 # etc (i have a LOT of data here)
    return test_data

what i get is the following:

  td = my_data()
  print td
  output : (1,2,3,4)

i want the function to act as it was doing the following code:

return 1,2,3,4

Is there a way to this?

EDIT!

i'm using this with python ddt

@ddt
class testsuite_my_function_validations(unittest.TestCase):

    @data(my_data())
    def test_various_number(self, value):
        test_number = value
        # test the number

It is suppose to have four test data (1, 2, 3 and 4) instead i'm getting a tuple value and not the those four numbers. But when i

return 1,2,3,4

The output will have four testcases:

  1. testcase test value 1
  2. testcase test value 2
  3. testcase test value 3
  4. testcase test value 4
2
  • What do you mean? You have a problem with the parens? Commented Jan 12, 2014 at 6:20
  • 5
    return 1, 2, 3, 4 is exactly the same with return test_data. Commented Jan 12, 2014 at 6:21

2 Answers 2

6

Returning multiple return values, like so:

return 1, 2, 3, 4

actually creates a tuple and returns it. It's exactly the same as

test_data = 1, 2, 3, 4
return test_data

In both cases, you can store the returned tuple in a variable or unpack it:

def my_data():
    test_data = 1, 2, 3, 4
    return test_data
a, b, c, d = my_data()

What you need to do is unpack the tuple in the decorator call with * notation:

@ddt
class testsuite_my_function_validations(unittest.TestCase):
#         v this star here
    @data(*my_data())
    def test_various_number(self, value):
        test_number = value
        # test the number

Putting a * before the last argument of a function call unpacks that argument as a sequence and feeds the sequence elements as separate arguments to the function. In other words,

f(1, *(2, 3, 4))

is equivalent to

f(1, 2, 3, 4)

Note that tuples are generally for fixed-length collections, often of inhomogenous data. If you have a large amount of data, lists are usually more suitable.

Sign up to request clarification or add additional context in comments.

Comments

0

Youre question is confusing on the output you are trying to achieve but,

you could use a list like this:

list1 = [1,2,3,4]

return list1

then you can acsess those values from on variable

More on lists

Comments

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.