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


Groups > comp.lang.python > #100381

Re: Calling a list of functions

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Calling a list of functions
Date Sun, 13 Dec 2015 18:40:28 +0100
Organization None
Lines 52
Message-ID <mailman.211.1450028441.12405.python-list@python.org> (permalink)
References <CACT3xuXP640BS3V+w1ABAjG5OvNWGb3yq6dEQZkZhdFaeUKP=A@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de IxP1x+TwMXcNhPlFYJGFKQ0t/QoXHQIhJaMaCECi+0rQ==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'func': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; '2.7': 0.13; 'exception': 0.13; 'def': 0.13; 'c++.': 0.16; 'literal.': 0.16; 'operator.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'integer': 0.18; 'try:': 0.18; 'team,': 0.18; 'pass': 0.22; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'linux': 0.26; 'function': 0.28; 'actual': 0.28; 'fine': 0.28; 'division': 0.29; 'raise': 0.29; 'print': 0.30; 'code': 0.30; 'run': 0.33; 'except': 0.34; 'list': 0.34; 'list:': 0.35; 'list,': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; "won't": 0.38; 'why': 0.39; 'test': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'share': 0.61; 'sample': 0.63
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd8794.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:100381

Show key headers only | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Calling a list of functions Peter Otten <__peter__@web.de> - 2015-12-13 18:40 +0100

csiph-web