2

Long-hand this is how it would look like:

class TestClass(object):
    def f(num):
        """In general, a complicated function."""
        return num

    self.a = f(1)
    self.b = f(2)
    self.c = f(3)
    self.d = f(4)
    self.e = f(5)

I'm thinking dictionary methods could help, but how?

4
  • how about this a, b, c, d, e = [f(i) for i in range(1,6)] Commented Oct 30, 2015 at 6:24
  • I find that if I copy-paste and then manually change the arguments, it's more prone to errors. For example, I can end up writing self.b = f(1) if I'm not careful. I'd like to avoid that as much as possible. Commented Oct 30, 2015 at 6:28
  • then I recommend lists array = [f(i) for i in range(1,6)] but if you like dict look at the Kasramvd answer Commented Oct 30, 2015 at 6:33
  • Thanks @Azad! I think the list is better for few variable names so they'd fit in the window. Commented Oct 30, 2015 at 6:40

2 Answers 2

1

As you said you better to use a dictionary.And as a more pythonic way you can use a dictionary comprehension.You can use enumerate to create a sequence of keys for your dictionary based on your items index. :

>>> my_dict = {'a{}'.format(i):f(j) for i,j in enumerate([3,4,5,1,2])}
{'a1': 4, 'a0': 3, 'a3': 1, 'a2': 5, 'a4': 2}

And for accessing to each value you can use a simple indexing :

>>> my_dict['a3']
1

Also if you want to use custom names for your keys you can use zip function to zip the variable names with values the use if within a dict comprehension:

>>> var_names=['a','b','c','d','e']
>>> values=[1,2,3,4,5]
>>> 
>>> my_dict = {i:f(j) for i,j in zip(var_names,values)}
>>> my_dict
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I'd rather keep them as separate variable names, but I think this is neater when I use more than 5 variables.
@irene Indeed, the first approach is better.
1

You're going in the wrong direction - if you want to assign several references based on the same function, you should be storing them in a data structure like a list instead of in discrete, manually-entered variables. You can unpack them like that later if you want, but you should start with a data structure. It then becomes easier to map() each value in an iterable to this function, and then turn it into a list.

def f(num):
    """In general, a complicated function."""
    return num

my_numbers = list(map(f, range(1, 6)))

Your numbers were a tidy range this time so I just used a range() object, but you can use any iterable you like, such as [1, 2, 3] or (4, 2, 3).

2 Comments

The map() function is new to me. I may have difficulty in bookkeeping (which index corresponds to which variable), but it's an interesting method nonetheless. Thanks a lot!
No problem. Basically, any time you find yourself manually creating variable names that follow a sequence, like label1, label2, label3, etc., you've got a problem, because adding elements means your script gets bigger and more difficult to work with. Note how the method above would take no more code for range(1, 1000) than it does for range(1, 6).

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.