Jump to content

How to execute the function to fill the list and to print it


zak100

Recommended Posts

Hi,

I got some in complet program from a book "aipython":

The code after some modification is:

fun_list1 = [ ]
for j in range(5):
    def fun1(e):
        return e + j
    fun_list1.append(fun1)
for x in fun_list1:
   print(x)

 

What I understand is that I have a for loop which is invokingthe function "fun1(e)" 5 times and the  return value is stored in a list.I trying to print the list, I am getting following output:

Quote

<function fun1 at 0x7f6d8a364e18>
<function fun1 at 0x7f6d8a28a158>
<function fun1 at 0x7f6d8a28a1e0>
<function fun1 at 0x7f6d8a28a268>
<function fun1 at 0x7f6d8a28a2f0>

Process finished with exit code 0

So I understand that I have to put this whole thing in a function and then pass some argument e  to the function, and then it will work. Some body please guide how to execute this program such that I get some answer for the list.

Zulfi.

Link to comment
Share on other sites

You are not invoking the function fun1(e) and storing the results in the list. You are storing five different functions fun1(e) in a list and never invoke them. Namely, the  functions  you store are fun1(e)=e+0, fun1(e)=e+1, fun1(e)=e+2, fun1(e)=e+3 and fun1(e)=e+4. What the print tells you is exactly this: Your list contains five functions named fun1 at the memory locations 0x7f6d8a364e18, 0x7f6d8a28a158 etc.

 

If you wanted to invoke the function and store its values you'd need to invoke the function and type something like fun_list1.append(fun1(42)) (in this case you invoke it with the parameter 42 - every other number would work, too). And of course writing fun_list1.append(42 + j) would be the cleaner code to do the same without the unnecessary function.

 

There are legitimate reasons why sometimes you want to store an actual function instead of the function result (you don't know the argument at the moment, you want to execute it multiple times, ....). You can call the stored function fun1(e) just like any other function. If you want to see evaluated results, change your print statement to print(x(42)) and your output should become 42, 43, 44, 45, 46 (again, the argument 42 is just a random number, and any other number should work).

Edited by timo
Link to comment
Share on other sites

Hi timo-

Thanks a lot. God blesses you.  I have one question

Quote

You are not invoking the function fun1(e) and storing the results in the list. You are storing five different functions fun1(e) in a list and never invoke them. Namely, the  functions  you store are fun1(e)=e+0, fun1(e)=e+1, fun1(e)=e+2, fun1(e)=e+3 and fun1(e)=e+4.

What you mean by the statement after '=' : fun1(e)= e +0, fun1(e)=e+0, fun1(e)=e+1, fun1(e)=e+2, fun1(e)=e+3 and fun1(e)=e+4.

Does it mean the code of function?

 

Zulfi.

Link to comment
Share on other sites

I meant the function in the sense of a mathematical function in this case. The point is just to highlight that you have five different functions. The code of the functions would be:

	def fun1(e):
	    return e+0
	

,

	def fun1(e):
	    return e+1
	

,

etc.

 

Except that the functions are not named fun1

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.