-2

Basically, I have a lot of functions that have a similar name (p1(), p2(), p3(), ..., p20()) and I don't want to call them all by hand. I want to do something like this:

for i in range(1, 21):
     p{i}()

But that doesn't seem to work. Is there some other way to go around this?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • You could do some crazy stuff with `globals()` to access objects/variables (functions are objects/variables) dynamically. Might be more sensible to create a data structure that contains your functions like a list or dict – Iain Shelvington Dec 27 '20 at 23:39
  • 3
    See https://stackoverflow.com/q/1373164/3001761. **TL;DR**: don't. – jonrsharpe Dec 27 '20 at 23:39
  • https://stackoverflow.com/questions/25799297/python-computing-a-function-name-from-another-function-name can help – Ruperto Dec 27 '20 at 23:42
  • 1
    Q: So what's the "relationship" between these (rather unimaginatively named) p1(), p2(), etc functions? Q: Would it make sense to have a "collection" (e.g. an array or dictionary) of function pointers: https://stackoverflow.com/a/2283243/421195? Q: Would it make sense to wrap them all in a class, then invoke them collectively in a single class method? – paulsm4 Dec 27 '20 at 23:44
  • Could you include some of these functions as part of the example? Are they all very short? Do you ever invoke them individually, or are they always invoked as a unit (and if so why are they separate functions in the first place)? – Samwise Dec 27 '20 at 23:44

3 Answers3

1

Functions can be put into a list like any other object, so you can build a list of functions and then iterate over it:

p_funcs = [p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20]
for f in p_funcs:
    f()

Note that needing to do things like this might be an indication that your life would be easier if you refactored these functions into a smaller number of more complex functions.

Samwise
  • 68,105
  • 3
  • 30
  • 44
  • I'm not sure why the OP named their functions so poorly, or if they're trying to do anything but save keystrokes with a poor design ... but this is a good solution. To a questionable question ;) – paulsm4 Dec 28 '20 at 00:05
0

A potential solution is to create a mechanism where your functions are registered/stored so that they can be iterated over.

Here is a solution that uses a decorator to "register" each function and store them in a list so that they can be iterated over

registered_funcs = []


def register_func(f):
    registered_funcs.append(f)
    return f


@register_func
def p1():
    print('p1')


@register_func
def p2():
    print('p2')


for func in registered_funcs:
    func()
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50
-2

This one is is based on eval cmd from Computing a function name from another function name.

def p1():
    print("testing function p1()")

def p2():
    print("testing function p2()")
    
for i in range(1, 3):
    eval('p' + str(i) + '()')

output

testing function p1()
testing function p2()
martineau
  • 119,623
  • 25
  • 170
  • 301
Ruperto
  • 326
  • 2
  • 8