Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Calling a list of functions Date: Sun, 13 Dec 2015 10:43:54 -0700 Lines: 51 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de YqOP2lsVzaqMvU2bdkePXw4NiGCJbQT4GkpYVP2wQIdQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.03; 'handler': 0.04; 'correct.': 0.07; 'iterate': 0.09; 'themselves,': 0.09; 'python': 0.10; '2.7': 0.13; 'exception': 0.13; 'def': 0.13; 'instead.': 0.15; '10:26': 0.16; 'correctly,': 0.16; 'iterating': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'test()': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'team,': 0.18; '2015': 0.20; 'am,': 0.23; 'dec': 0.23; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'linux': 0.26; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'values': 0.28; 'fine': 0.28; 'looks': 0.29; '13,': 0.29; 'print': 0.30; 'code': 0.30; 'run': 0.33; 'usually': 0.33; 'except': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'functions.': 0.35; 'quite': 0.35; 'list,': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:209.85.213': 0.37; 'list.': 0.37; 'received:209': 0.38; 'building': 0.38; 'test': 0.39; 'skip:e 20': 0.39; 'build': 0.40; 'to:addr:python.org': 0.40; 'called': 0.40; 'some': 0.40; 'your': 0.60; 'share': 0.61; 'above,': 0.63; 'sample': 0.63; 'body.': 0.66; 'here': 0.66; 'results': 0.66; 'cut': 0.67; 'to:name:python': 0.84; 'do:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=BPeFnp/BC7R1EXMi6aAi+rc5LG2CPXrvCblKuaCvz0M=; b=hrMhexdoDz92sAB5LUOr0uYYMxjtxalxaQlTzvdKoV9j8LDi42gT0XqLdwiZZZvTyq f4aWPWWE/2p12fOt5YvYqhmL+s63q//Rd1upHxHQsnXQhARyGBpW14NqqdgMje3eTrPd CEZDkv8g934tZRUE1nFQG9Gqa0NodJLIwDYbKN7VCU9+aXlNWhNrlX1lZf5DngE7GLEy tEnz3awNH6f7bTZpfKcEx6riu1z/Uu2hWP8nHMzT01Sy04Ptnx1hNqxiY7+LM0CoHfdd DtPHatxJM3TzGLQ/TN6k5idPQqKDUG8IH1Qqqv1Cr/KCFyHpvFJpGOWXPdvqB22+6j3e z3fA== X-Received: by 10.50.138.136 with SMTP id qq8mr11642944igb.68.1450028673851; Sun, 13 Dec 2015 09:44:33 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100383 On Sun, Dec 13, 2015 at 10: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 , 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 If I understand correctly, you want to build the list of functions and then call them later. If so, this is not quite correct. This is calling the functions at the time that you build the list and placing the return values in the list, not the functions. To build a list of the functions themselves, do: print_test = [print1, print2, print3] > for test in range(len(print_test)): Iterating over range(len(something)) is usually not correct. Just iterate over print_test instead. If you really need the indexes, then iterate over enumerate(print_test). > try: > print_test[test] > except AssertionError as exc: It looks like some code got cut off here since your exception handler has no body. Regardless, the exception handler will never be invoked because print_test[test] is just looking up the results of the functions that were called when building the list. To actually call the functions here instead of above, do: for test in print_test: test()