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


Groups > comp.lang.python > #91272

Re: Array of Functions

Path csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gary.herron@islandtraining.com>
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; 'else:': 0.03; 'performs': 0.07; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'python': 0.11; 'python.': 0.11; 'appropriate': 0.14; 'def': 0.14; 'java,': 0.15; 'languages,': 0.15; '1):': 0.16; 'parameters,': 0.16; 'received:67.192.241.150': 0.16; 'received:smtp150.dfw.emailsrvr.com': 0.16; 'simplified': 0.16; 'wrote:': 0.16; 'solution.': 0.18; 'compare': 0.20; 'c++,': 0.22; 'function,': 0.22; 'nested': 0.22; 'next,': 0.22; 'parameter': 0.22; 'phd': 0.22; 'received:emailsrvr.com': 0.22; 'posted': 0.23; 'header:In-Reply-To:1': 0.24; 'example': 0.25; 'header:User- Agent:1': 0.26; 'earlier': 0.27; 'dictionary': 0.29; 'finds': 0.29; 'array': 0.29; 'function': 0.30; 'becomes': 0.31; 'received:(smtp server)': 0.31; 'anyone': 0.32; 'problem': 0.33; 'decorators': 0.33; 'another': 0.34; 'to:addr:python-list': 0.35; 'question,': 0.35; 'solving': 0.35; 'really': 0.35; 'problem.': 0.35; 'but': 0.36; 'too': 0.36; 'list,': 0.36; 'there': 0.36; 'so,': 0.37; 'two': 0.37; 'subject:: ': 0.37; 'list.': 0.37; 'level': 0.37; 'associated': 0.38; 'someone': 0.38; 'wanted': 0.39; 'pm,': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'called': 0.40; 'easy': 0.60; 'hope': 0.61; 'here.': 0.61; 'more': 0.62; 'enclosed': 0.63; 'within': 0.64; 'charset:windows-1252': 0.65; 'here': 0.66; 'fact,': 0.67; 'confusing': 0.84; 'dimensional': 0.84
X-Sender-Id gary.herron@islandtraining.com
Date Tue, 26 May 2015 18:18:04 -0700
From Gary Herron <gary.herron@islandtraining.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Array of Functions
References <42b534f0-aeef-411f-80c5-db5d20c80b55@googlegroups.com> <TbKdnVQBR8OujvjInZ2dnUU7-LOdnZ2d@giganews.com>
In-Reply-To <TbKdnVQBR8OujvjInZ2dnUU7-LOdnZ2d@giganews.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
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>
Newsgroups comp.lang.python
Message-ID <mailman.70.1432690002.5151.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432690002 news.xs4all.nl 2867 [2001:888:2000:d::a6]:40182
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91272

Show key headers only | View raw


On 05/26/2015 05:43 PM, richard_riehle wrote:
> I realized that I mentioned earlier that I found a solution to my original question, but that I never posted an example of the solution.   So, here is a simplified example for anyone who is interested.
>
> def fArray(fselect, fparm  = 1):
> 	def A1(p = fparm):
> 		if p == 1:
> 			print("printing A1[1]")
> 		else:
> 			print("printing A1[other]")
> 	def A2(p = fparm):
> 		if p == 1:
> 			print("printing A2[1]")
> 		else:
> 			print("printing A2[other]")
> 	A = [A1, A2]
> 	A[fselect]()

Nested functions are often confusing and unclear, and there is really no 
need for them here.   This does the same thing:


def A1(p):
     print("printing",  "A1[1]" if p==1 else "A1[other]"))

def A2(p):
     print("printing",  "A2[1]" if p==1 else "A2[other]"))

def fArray(fselect, fparm=1):
     A = [A1,A2]
     A[fselect](fparm)


> In this example, I enclosed two functions within another function, and then put those two functions in a list.  Then, with appropriate parameters, I called one of the functions in the list, associated the formal parameter with the function in a call to the array, and presto, it performs the function.
>
> The more advanced problem I wanted to solve, a two dimensional array of functions, once this example is understood, becomes trivial to implement.  In fact, the more interesting problem I wanted to solve involved a dictionary of functions in a two-dimensional array, and that too was easy to do in Python.
>
> When I compare what this would require in C, C++, Java, or most other languages, I find Python to be really easy for solving this kind of problem.
>
> Next, I plan to develop the solution using decorators and assertions to empower it with a greater level of portability and to make the functions more generic.
>
> I hope someone finds this interesting.
>
> Richard Riehle, PhD
>
>

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Array of Functions richard_riehle <rriehle@itu.edu> - 2015-05-26 19:43 -0500
  Re: Array of Functions Laura Creighton <lac@openend.se> - 2015-05-27 03:05 +0200
  Re: Array of Functions Gary Herron <gary.herron@islandtraining.com> - 2015-05-26 18:18 -0700

csiph-web