0

I would like to be able to do the following, in Python 2.7:

    for function in ['function1', 'function2', 'function3', 'function4']:
        some_dictionary [function] = function (an_argument)

However, when I try to do so, an error rises. The following code does the thing I would like the upper code to do:

    some_dictionary ['function1'] = function1 (an_argument)
    some_dictionary ['function2'] = function2 (an_argument)
    some_dictionary ['function3'] = function3 (an_argument)
    some_dictionary ['function4'] = function4 (an_argument)

Is there a more compact way of writing the latter code, something similar to the former one?

Thanks in advance,

Logicum

3
  • Where are function1,function2,function3,function4 defined? In some specific file you're importing? Commented Jun 1, 2014 at 17:03
  • Nonetheless, relevant: stackoverflow.com/questions/3061/… Commented Jun 1, 2014 at 17:03
  • In this specific case, I have defined those functions myself, prior to calling them. All the functions accept one argument of the same type, here called "an_argument". Commented Jun 1, 2014 at 17:06

3 Answers 3

5
for function in ['function1', 'function2', 'function3', 'function4']:
    some_dictionary[function] = function(an_argument)

This won't work because the items in the list are strings, and you can't call strings. Instead, put the actual functions in the list.

for function in [function1, function2, function3, function4]:
    some_dictionary[function.__name__] = function(an_argument)

You can also use the function itself as a key, which can be useful in some circumstances (for example, you can put a description of the function in its docstring and use that to print something nice for the user, or you can allow the user to retry the function, or whatever): It's much simpler to keep the function reference for later use, in other words, than to try to get it from the function name.

for function in [function1, function2, function3, function4]:
    some_dictionary[function] = function(an_argument)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use func_name:

>>> a = lambda x: x + 1
>>> b = lambda y: y + 2
>>> c = lambda z: z + 3
>>> a.func_name = 'a'
>>> b.func_name = 'b'
>>> c.func_name = 'c'
>>> z = a, b, c
>>> d = {}
>>> for f in z:
...     d[f.func_name] = f(10)
...
...
>>> d
{'a': 11, 'c': 13, 'b': 12}

Using lambdas here, so everything is name <lambda>, but with proper functions, their func_names will reflect their variable names.

Comments

0

If you have the names of the functions as strings, you can use locals() to access them:

def function1():
    print "in function1"

def function2():
    print 'in function2'

some_dictionary = {}

for func in ['function1', 'function2']:
    some_dictionary[func] = locals()[func]

print some_dictionary['function1']()

prints:

in function1

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.