0

Is there a way to create variable variables in Python for the following specific case: say I have a function that outputs lots of objects (a fixed, but large, number, say 20), like

a1, a2, a3, a4, ..., a20 = f(20),

where a1, a2, a3, ..., a20 are objects.

There have been many questions asked on this topic in SE, where people generally say to use lists or dictionaries. What I don't understand is how to use lists/dictionaries in this specific case.

If I had:

a1 = g(1)
a2 = g(2)
...

I would understand how to do this, but I'm not sure in the above case how to "capture" all the objects into a list without providing explicitly all the variable names.

3
  • 2
    Python function can return one value. So it's a little confusing when you say, "function that outputs lots of objects". Do you mean it outputs a list of objects? It would be really helpful if you could show a minimal example of your problem. Commented Nov 2, 2020 at 1:43
  • 1
    if your function returned lots of objects... they are already in a container - likely a tuple. You could do foo = f(20) and then, say, a2 would be foo[1]. Commented Nov 2, 2020 at 1:46
  • 1
    Ah, thanks! I was thinking about matplotlib's plt.subplots, with fig, (ax1, ax2, ax3, ...) = plt.subplot(). But I just looked at the documentation, and I can use fig, ax = plt.subplot() to get the whole thing instead. How do I accept the comment as an answer? Commented Nov 2, 2020 at 1:50

3 Answers 3

1

A python function always returns a single object. That object could be None, a single value, or a container holding values. When returning a container, python lets you unpack that container into multiple variables or keep it a single variable.

You can unpack

>>> def f(val):
...     return list(range(val))
... 
>>> a1, a2, a3 = f(3)

Or just keep the container

>>> foo = f(3)
>>> type(foo)
<class 'list'>
>>> foo
[0, 1, 2]
>>> a2 == foo[1]
True
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure if you're asking how to access your 20 values once they're returned, or how to return 20 different values.

I will say, more as a point of advice, that most times you're trying to return a data structure with 20 parts, it might be easier to make that a class, and return a class.

But, that doesn't answer your question. For this example, assuming that you really just want a list of 20 things passed through, I suggest tuples. See the example below for a function that creates a tuple, and that same tuple being unpacked earlier.

# My function get_information returns the latitude,
# longitude, time, and number of likes a given post has

def get_post(post_id):
    # Get post information here
    long = 12.4213
    lat = 12351.34
    time = '12:00'
    num_likes = -3
    # Now to return all these values
    return ( long, lat, time, num_likes )

# Outside of the function, we can access the values in this way
my_long, my_lat, my_time, my_num_likes = get_post(10)
# Python unpacks the tuple into these variables in the order they are put into the tuple.

Comments

1

Say, you have your function that returns a tuple of 20 objects, here you don't need to create many variables to hold these objects, but instead you can use a dictionary for example:

# Say it's your function
def f(x):
    return a0, a1, a2, ..., a19

Then you can easily place all return values in a dictionary like this:

foo = f(x)
dic = {f'a{i}': foo[i] for i in range(len(foo))}

And here you will get a dictionary like:

dic = {'a0': first_return_value,
       'a1': second_return_value,
       etc,}

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.