Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100381 > unrolled thread
| Started by | Peter Otten <__peter__@web.de> |
|---|---|
| First post | 2015-12-13 18:40 +0100 |
| Last post | 2015-12-13 18:40 +0100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Calling a list of functions Peter Otten <__peter__@web.de> - 2015-12-13 18:40 +0100
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-12-13 18:40 +0100 |
| Subject | Re: Calling a list of functions |
| Message-ID | <mailman.211.1450028441.12405.python-list@python.org> |
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
Python is not C++. In Python // is the integer division operator.
> for test in range(len(print_test)):
> try:
> print_test[test]
> except AssertionError as exc:
1 Why range()?
Iterate directly over the list:
for result in print_test:
print result
2 Why the try...except?
If there are functions in your actual code that may raise an AssertionError
you won't catch it in the loop because you already invoked the functions
when you built the list literal. To catch an exception like that you have to
put the functions into the list, not the results:
functions = [print1, print2, print3]
for func in functions:
try:
print func()
except AssertionError:
pass
Back to top | Article view | comp.lang.python
csiph-web