0

"I'am making a list of functions in python but when i run the program the all functions in the list are get automatically called."

"I've tried it on python 3.7.3 and the same concept i also tried in C++ but there also it does the same."

    def a():
        print ('a')
    def b():
        print('b')
    def c():
        print('c')
    def d():
        print('d')
    aa=[a(),b(),c(),d()]
    print(aa[1])

"I expect the output of aa[1] to be 'b', but in output all the function get called and it gives output a b c d None "

MSY
  • 23
  • 1
  • 5
  • 1
    your functions don't return anything. – Paritosh Singh Jun 12 '19 at 17:16
  • change your `print(#)` to `return #` – depperm Jun 12 '19 at 17:16
  • 2
    You explicitly call all your functions here: `aa=[a(),b(),c(),d()]`. – juanpa.arrivillaga Jun 12 '19 at 17:17
  • Your functions *are* run, though not automatically; you explicitly call them in the expression that defines `aa`. The problem is that you are confusing standard output with the value of a function call. – chepner Jun 12 '19 at 17:17
  • See this lovely [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) blog for help. Also try working through a tutorial on function calls. These should help clear up a couple of your problems, and reduce the confusion to one or two points you can handle. – Prune Jun 12 '19 at 17:28

3 Answers3

1

your functions do not return anything. They only print a letter and return None.

use

def a():
    print ('a')
    return 'a'

...

instead

Raphael
  • 1,731
  • 2
  • 7
  • 23
1

You explicitly called all the functions when you wrote [a(), b(), c(), d()] so all of them running (in this case printing a letter) is the expected output indeed. I believe what you want is to collect the callables in a list first without calling them. If you want then a desired function out of that list to run by indexing, you can do so by calling only afterward.

For example,

def a():
    print ('a')
def b():
    print('b')
def c():
    print('c')
def d():
    print('d')

# placeholder for the functions
funcs = [a, b, c, d]

Then you can call any of the functions by indexing:

# call any of them by indexing whenever you feel like it.
funcs[1]()

Output: b
WiseDev
  • 524
  • 4
  • 17
1

Use return instead of print. When you create your list aa, each element is being called immediately, because you've written () after each element - so consequently each print statement is being run as your list is built. Additinally, when a function doesn't explicitly return a value, it returns None. When you print(aa[1]), you're then printing the return value of b(), which is None. So altogether you get:

a     # because it's printed in a()
b     # because it's printed in b()
c     # because it's printed in c()
d     # because it's printed in d()
None  # because it's returned from b()

If you use return instead of print in each function, each element of the list will then contain the value returned by each function (your list would effectively become aa = ['a', 'b', 'c', 'd']). Then, when you then print(aa[1]), you'd print the value of aa in position 1, which would be 'b'.

def a():
    return 'a'
def b():
    return 'b'
def c(): 
    return 'c' 
def d():
    return 'd'
aa=[a(),b(),c(),d()] 
print(aa[1])

Alternatively, if you wanted to keep your print statements in each function, you could build your list with references to each function instead of calling each function - and then run the result:

def a():
    print ('a')
def b():
    print('b')
def c():
    print('c')
def d():
    print('d')
aa=[a,b,c,d]
print(aa[1]())

By putting () after the aa[1], you're saying, try to run whatever is returned from aa[1]. Since aa[1] contains a reference to function b, you're effectively calling b() at that point, which will then run your print('b') statement.

CatamountJack
  • 150
  • 2
  • 5