Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100379 > unrolled thread

Calling a list of functions

Started byGanesh Pal <ganesh1pal@gmail.com>
First post2015-12-13 22:56 +0530
Last post2015-12-13 21:54 -0800
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Calling a list of functions Ganesh Pal <ganesh1pal@gmail.com> - 2015-12-13 22:56 +0530
    Re: Calling a list of functions BartC <bc@freeuk.com> - 2015-12-13 17:38 +0000
    Re: Calling a list of functions Grant Edwards <invalid@invalid.invalid> - 2015-12-13 17:41 +0000
    Re: Calling a list of functions Steven D'Aprano <steve@pearwood.info> - 2015-12-14 13:43 +1100
      Re: Calling a list of functions Chris Angelico <rosuav@gmail.com> - 2015-12-14 13:49 +1100
    Re: Calling a list of functions Anand <abapat@gmail.com> - 2015-12-13 21:54 -0800

#100379 — Calling a list of functions

FromGanesh Pal <ganesh1pal@gmail.com>
Date2015-12-13 22:56 +0530
SubjectCalling a list of functions
Message-ID<mailman.210.1450027598.12405.python-list@python.org>
Hi Team,

Iam on linux and python 2.7  . I have a bunch of functions  which I
have run sequentially .
I have put them in a list and Iam calling the functions in the list as
shown below ,  this works fine for me , please share your
opinion/views on the same


Sample code :

def print1():
    print "one"

def print2():
    print "two"

def print3():
    print "three"

print_test = [print1(),print2(),print3()] //calling the function

for test in range(len(print_test)):
  try:
      print_test[test]
  except AssertionError as exc:


Regards,
Ganesh

[toc] | [next] | [standalone]


#100380

FromBartC <bc@freeuk.com>
Date2015-12-13 17:38 +0000
Message-ID<n4kaa7$35n$1@dont-email.me>
In reply to#100379
On 13/12/2015 17:26, Ganesh Pal wrote:

> Iam on linux and python 2.7  . I have a bunch of functions  which I
> have run sequentially .
> I have put them in a list and Iam calling the functions in the list as
> shown below ,  this works fine for me , please share your
> opinion/views on the same
>
>
> Sample code :
>
> def print1():
>      print "one"
>
> def print2():
>      print "two"
>
> def print3():
>      print "three"
>
> print_test = [print1(),print2(),print3()] //calling the function
>
> for test in range(len(print_test)):
>    try:
>        print_test[test]
>    except AssertionError as exc:
>

 > I have put them in a list and Iam calling the functions in the list as
 > shown below ,  this works fine for me , please share your

That's not quite what the code does, which is to call the three 
functions and put their results into the list (3 Nones I think).

Then you evaluate each element of the list (a None each time).

I had to modify it to the following, which sets up a list of the three 
functions, then calls them in turn using the loop. I don't know what the 
'except' part was supposed to do:

def print1():
     print "one"

def print2():
     print "two"

def print3():
     print "three"

print_test = [print1,print2,print3]	#NOT calling the function

for test in range(len(print_test)):
     try:
     	print_test[test]()              #calling the function
     except AssertionError:
     	pass

The output of the two programs would have been the same I think.

-- 
Bartc

[toc] | [prev] | [next] | [standalone]


#100382

FromGrant Edwards <invalid@invalid.invalid>
Date2015-12-13 17:41 +0000
Message-ID<n4kak7$n9h$1@reader1.panix.com>
In reply to#100379
On 2015-12-13, Ganesh Pal <ganesh1pal@gmail.com> wrote:
> Hi Team,
>
> Iam on linux and python 2.7 . I have a bunch of functions which I
> have run sequentially . I have put them in a list and Iam calling the
> functions in the list as shown below , this works fine for me ,
> please share your opinion/views on the same
>
> Sample code :
>
> def print1():
>     print "one"
>
> def print2():
>     print "two"
>
> def print3():
>     print "three"
>
> print_test = [print1(),print2(),print3()] //calling the function
>
> for test in range(len(print_test)):
>   try:
>       print_test[test]
>   except AssertionError as exc:

I have no clue what your actual goal is, but it might be better to do
the function call in the try/except block inside the loop. Otherwise
your try/except block makes no sense because there's nothing being
executed inside it:

for test in [print1,print2,print3]:
    try:
        test()
    except AssertionError as exc:
        print exc

        



[toc] | [prev] | [next] | [standalone]


#100403

FromSteven D'Aprano <steve@pearwood.info>
Date2015-12-14 13:43 +1100
Message-ID<566e2cbc$0$1590$c3e8da3$5496439d@news.astraweb.com>
In reply to#100379
On Mon, 14 Dec 2015 04:26 am, Ganesh Pal wrote:

> Hi Team,
> 
> Iam on linux and python 2.7  . I have a bunch of functions  which I
> have run sequentially .
> I have put them in a list and Iam calling the functions in the list as
> shown below ,  this works fine for me , 

No it doesn't. It doesn't even compile -- it gives a SyntaxError because you
incorrectly use // as a comment deliminator instead of #. After fixing
that, you get a second SyntaxError because your code has an empty "except"
block. Obviously the code you show us is not the code you ran.

Why do people do this?

"Hi, here's a cake a made earlier, I think it tastes really nice. What do
you think?"

"That's not a cake. It's a bowl of mud with a cherry on top. Where is the
actual cake?"


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#100404

FromChris Angelico <rosuav@gmail.com>
Date2015-12-14 13:49 +1100
Message-ID<mailman.1.1450061347.14916.python-list@python.org>
In reply to#100403
On Mon, Dec 14, 2015 at 1:43 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> Why do people do this?
>
> "Hi, here's a cake a made earlier, I think it tastes really nice. What do
> you think?"
>
> "That's not a cake. It's a bowl of mud with a cherry on top. Where is the
> actual cake?"

Steven, haven't you ever had a mudcake before?

ChrisA

[toc] | [prev] | [next] | [standalone]


#100406

FromAnand <abapat@gmail.com>
Date2015-12-13 21:54 -0800
Message-ID<ef8e07e6-c13a-4b1b-a38e-a52099ae5240@googlegroups.com>
In reply to#100379
On Sunday, December 13, 2015 at 9:26:52 AM UTC-8, Ganesh Pal wrote:
> Hi Team,
> 
> Iam on linux and python 2.7  . I have a bunch of functions  which I
> have run sequentially .
> I have put them in a list and Iam calling the functions in the list as
> shown below ,  this works fine for me , please share your
> opinion/views on the same
> 
> 
> Sample code :
> 
> def print1():
>     print "one"
> 
> def print2():
>     print "two"
> 
> def print3():
>     print "three"
> 
> print_test = [print1(),print2(),print3()] //calling the function
> 
> for test in range(len(print_test)):
>   try:
>       print_test[test]
>   except AssertionError as exc:
> 
> 
> Regards,
> Ganesh




def print1(): 
     print "one" 
 
 def print2(): 
>     print "two" 
> 
> def print3(): 
>     print "three" 
> 
> print_test = [print1(),print2(),print3()] //calling the function 


If the idea is to have a 'pointers to function array' (as in C), you can do this:

fun_arr=[print1,print2,print3]
# Execute now
[ f() for f in fun_arr ]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web