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


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

Re: Array of Functions

Started byrichard_riehle <rriehle@itu.edu>
First post2015-05-26 19:43 -0500
Last post2015-05-26 18:18 -0700
Articles 3 — 3 participants

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.


Contents

  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

#91270 — Re: Array of Functions

Fromrichard_riehle <rriehle@itu.edu>
Date2015-05-26 19:43 -0500
SubjectRe: Array of Functions
Message-ID<TbKdnVQBR8OujvjInZ2dnUU7-LOdnZ2d@giganews.com>
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]()

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

[toc] | [next] | [standalone]


#91271

FromLaura Creighton <lac@openend.se>
Date2015-05-27 03:05 +0200
Message-ID<mailman.69.1432688744.5151.python-list@python.org>
In reply to#91270
In a message of Tue, 26 May 2015 19:43:31 -0500, richard_riehle writes:
>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]()
>
>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

I am happy you found a solution, but I fear that adding decorators and
assertions will not do what you are asking for.  I missed the
original question, but it looks to me as if what you were looking
for is a nice python dictionary with a tuple as an index.  Since
tuples are immutable, they can be used as indices, while arrays
cannot.

Or, despite finding it interesting, have I misunderstood what
you are doing altogether?

Laura

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


#91272

FromGary Herron <gary.herron@islandtraining.com>
Date2015-05-26 18:18 -0700
Message-ID<mailman.70.1432690002.5151.python-list@python.org>
In reply to#91270
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
>
>

[toc] | [prev] | [standalone]


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


csiph-web