473,394 Members | 1,751 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

looping to define a sequence of functions?

Suppose we want to define ten functions such that the
nth function of x simply returns x**n. Is there a way
to avoid explicitly writing ten function def's? (They
have to be ten distinct functions, not just a single
one like def f(n,x): return x**n.)

E.g., the following doesn't work:

f = {}
for n in range(10):
def f[n](x):
return x**n

Thanks.
--
r.e.s.
Jul 18 '05 #1
3 1452
r.e.s. wrote:
Suppose we want to define ten functions such that the
nth function of x simply returns x**n. Is there a way
to avoid explicitly writing ten function def's? (They
have to be ten distinct functions, not just a single
one like def f(n,x): return x**n.)


functions = [lambda x, n=n: x**n for n in range(10)]

-or-

class X(object):
def __init__(self, n):
self.n = n
def __call__(self, x):
return x ** self.n
functions = [X(n) for n in range(10)]

-or-

functions = []
for n in range(10):
def f(x, n=n):
return x**n
functions.append(f)

-or-

def create_function(n):
def f(x):
return x**n
return f
functions = [create_function(n) for n in range(10)]
--
Rainer Deyke - ra*****@eldwood.com - http://eldwood.com
Jul 18 '05 #2
"Rainer Deyke" <ra*****@eldwood.com> wrote ...
r.e.s. wrote:
Suppose we want to define ten functions such that the
nth function of x simply returns x**n. Is there a way
to avoid explicitly writing ten function def's? (They
have to be ten distinct functions, not just a single
one like def f(n,x): return x**n.)


functions = [lambda x, n=n: x**n for n in range(10)]

-or-

class X(object):
def __init__(self, n):
self.n = n
def __call__(self, x):
return x ** self.n
functions = [X(n) for n in range(10)]

-or-

functions = []
for n in range(10):
def f(x, n=n):
return x**n
functions.append(f)

-or-

def create_function(n):
def f(x):
return x**n
return f
functions = [create_function(n) for n in range(10)]
--
Rainer Deyke - ra*****@eldwood.com - http://eldwood.com


It's going to take me a bit to digest these, and
it looks like I'm sure to learn from them, so I
want to say 'thank you' now for the quick reply.
Jul 18 '05 #3
"r.e.s." <r.*@ZZmindspring.com> wrote in message news:<T9*******************@newsread2.news.pas.ear thlink.net>...
Suppose we want to define ten functions such that the
nth function of x simply returns x**n. Is there a way
to avoid explicitly writing ten function def's? (They
have to be ten distinct functions, not just a single
one like def f(n,x): return x**n.)

E.g., the following doesn't work:

f = {}
for n in range(10):
def f[n](x):
return x**n

Thanks.

# A parametric factory-function that returns
# a function depending on the parameter n
def factory_function(n): .... def fn(x):
.... return x**n
.... return fn
.... # A list of ten different functions
func_list=map(factory_function,range(10))
# And an example of the result with x=2
for i,func in zip(range(len(func_list)),func_list): .... print '%2d: %d' % (i,func(2))
....
0: 1
1: 2
2: 4
3: 8
4: 16
5: 32
6: 64
7: 128
8: 256
9: 512


Regards
Peter
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Dennis | last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array float *myarray = new float ; of course I get an error. How do you allocate a 2D array using the New operator? I...
8
by: Terry Olsen | last post by:
How do I loop back to the beginning of a for/next loop before getting to the end of it? Isn't there an "iterate" command or something like that? For Each This in That ...code if This = False...
7
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I...
29
by: Ancient_Hacker | last post by:
It sure would be nice if I could have a macro that add a level of indirection to its argument. So if I write: AddIndirection( X ) The macro AddIndirection will do: #define X (*X) ...
16
by: Eigenvector | last post by:
Probably a dumb question, but I'm having problems with a while loop that I would like to have execute in some code I'm writing. Now what I'll post below is made up trivial code because what I'm...
9
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
6
by: Defcon2030 | last post by:
<bHey, can someone help me with this? I've been working on it for a few days now, and my head's starting to spin... </b> // FILE:ex1_imp.cxx // // // // CLASS IMPLEMENTED: sequence (see ex1.h...
16
by: kaferro | last post by:
What is the typical way to loop through a vector while deleting certain elements during the loop process? The code below works, but I am wondering if there is a better solution. ...
103
by: aboxylica | last post by:
hey! I have a program that takes two input files(one in the matrix form) and one in the sequence form.Now my problem is that i have to give the matrix file(containing many matrices) and sequence...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.