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


Groups > comp.lang.python > #80791

Re: Array of Functions

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!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 <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'example:': 0.03; 'languages,': 0.04; 'c++,': 0.07; 'column': 0.07; 'suppose': 0.07; 'noted,': 0.09; 'pointers': 0.09; 'rows': 0.09; 'assume': 0.14; 'behave': 0.16; 'operates': 0.16; 'pointers.': 0.16; 'repl': 0.16; 'spreadsheet.': 0.16; 'url:faq': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'bit': 0.19; '>>>': 0.22; 'programming': 0.22; 'button,': 0.24; 'faq': 0.24; 'java': 0.24; 'defined': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'array': 0.29; 'related': 0.29; 'nature': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'clever': 0.31; 'this.': 0.32; 'languages': 0.32; 'url:python': 0.33; 'fri,': 0.33; 'problem': 0.35; 'common': 0.35; 'except': 0.35; 'received:google.com': 0.35; '14,': 0.36; "didn't": 0.36; 'url:org': 0.36; 'example,': 0.37; 'two': 0.37; 'easily': 0.37; 'challenging': 0.38; 'nov': 0.38; 'richard': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'functional': 0.39; 'help,': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'most': 0.60; 'url:3': 0.61; "you're": 0.61; "you'll": 0.62; 'kind': 0.63; 'more': 0.64; 'difficulty': 0.68; 'answer:': 0.84; 'family.': 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:content-transfer-encoding; bh=bg5R+IRmDevZ2WyHPq7yGwphufV/WkJG0EBIuJrQuPM=; b=GQ3Fik/ltVvWUfhv00cuGENft5pLseb5lWvMTCwBXXrqzXJi3amK85l1rZQA+pZm9N Jz6efm3EBnmeV7gCIc8Z8RjvHUf01YLYziA+2lk682j87ubby7zff2/PuqXiYnUXos4N fy3axz3uKo0K9mrcPOfI6RTtUJHyhw7bJNKPIQ9+rs6woMCUM+GM9dTFwq9WgTIUEa+8 OSRiSAdqK7Fqy1LygD7McZ7w7+HWhrc8NoZrofSINDugYCoNRtEZENb4eaLfSzd1gP3g 7Mt+rCRiDLIJnIZKDK3xVhpyQbW5m++DIKrWErb4RnGOW7MwCsiweHFPAZLzEJOsRy5G KFsQ==
X-Received by 10.70.102.234 with SMTP id fr10mr13034863pdb.145.1416005483521; Fri, 14 Nov 2014 14:51:23 -0800 (PST)
MIME-Version 1.0
In-Reply-To <42b534f0-aeef-411f-80c5-db5d20c80b55@googlegroups.com>
References <42b534f0-aeef-411f-80c5-db5d20c80b55@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 14 Nov 2014 15:50:42 -0700
Subject Re: Array of Functions
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.15832.1416005493.18130.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416005493 news.xs4all.nl 2937 [2001:888:2000:d::a6]:54150
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80791

Show key headers only | View raw


On Fri, Nov 14, 2014 at 3:17 PM, Richard Riehle <rriehle@itu.edu> wrote:
> In C, C++, Ada, and functional languages, I can create an array of functions, albeit with the nastiness of pointers in the C family.   For example, an array of functions where each function is an active button, or an array of functions that behave like formulae in a spreadsheet.  I am finding this a bit challenging in Python.
>
> Example:
>
>             r1c1   r1c2  r1c3
>             r2c1   r2c2  r2c3
>             r3c1   r3c2  r3c3
>
> where r1 is row 1 and c1 is column 1.  Suppose I want an array where the colum three is a set of functions that operates on the other two columns, depending on the values I set for those rows and columns?    As noted, I can do this pretty easily in most languages (well, except for Java which does not support any kind of functional programming capability), even if I have to use pointers.
>
> I think my difficulty is related to the REPL nature of Python.  However, I am sure some clever Pythonista has found a way to do this.

>>> list_of_functions = [lambda x=x: x + 4 for x in range(3)]
>>> list_of_functions[0]()
4
>>> list_of_functions[1]()
5
>>> list_of_functions[2]()
6

If you want more help, you'll have to be more specific about the
problem you're encountering. Since you didn't go into any detail, I'm
just going to assume that it's the common pitfall of variable scoping
in functions defined in a loop and point you to this FAQ answer:

https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result

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


Thread

Array of Functions Richard Riehle <rriehle@itu.edu> - 2014-11-14 14:17 -0800
  Re: Array of Functions Marko Rauhamaa <marko@pacujo.net> - 2014-11-15 00:21 +0200
  Re: Array of Functions Sam Raker <sam.raker@gmail.com> - 2014-11-14 14:44 -0800
  Re: Array of Functions Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-14 15:50 -0700
  Re: Array of Functions MRAB <python@mrabarnett.plus.com> - 2014-11-14 23:02 +0000
  Re: Array of Functions Terry Reedy <tjreedy@udel.edu> - 2014-11-14 19:11 -0500
  Re: Array of Functions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-14 19:13 -0500
    Re: Array of Functions Richard Riehle <rriehle@itu.edu> - 2014-11-16 23:52 -0800
  Re: Array of Functions Anton <anschatten@gmail.com> - 2014-11-17 13:56 -0800

csiph-web