Path: csiph.com!usenet.pasdenom.info!news.etla.org!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'example:': 0.03; 'languages,': 0.04; 'c++,': 0.07; 'column': 0.07; 'suggestions,': 0.07; 'suppose': 0.07; 'advance': 0.07; 'function:': 0.09; 'noted,': 0.09; 'pointers': 0.09; 'references.': 0.09; 'rows': 0.09; 'def': 0.12; 'behave': 0.16; 'bind': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'help?': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'operates': 0.16; 'pointers,': 0.16; 'pointers.': 0.16; 'received:192.168.1.4': 0.16; 'repl': 0.16; 'spreadsheet.': 0.16; 'wrote:': 0.18; 'bit': 0.19; '>>>': 0.22; 'programming': 0.22; 'creating': 0.23; 'header :User-Agent:1': 0.23; 'button,': 0.24; 'java': 0.24; 'define': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'array': 0.29; 'related': 0.29; 'nature': 0.30; 'clever': 0.31; 'this.': 0.32; 'languages': 0.32; 'another': 0.32; 'except': 0.35; 'but': 0.35; 'functions.': 0.36; 'thanks': 0.36; 'example,': 0.37; 'two': 0.37; 'list': 0.37; 'easily': 0.37; 'challenging': 0.38; 'richard': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'functional': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'even': 0.60; 'most': 0.60; 'name:': 0.61; "you're": 0.61; 'name': 0.63; 'kind': 0.63; 'difficulty': 0.68; 'family.': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=ZuVjKrLG c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=aoAcgTy3oA8A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=4Z_t_BK2bRo88V0v3OMA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Fri, 14 Nov 2014 23:02:04 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Array of Functions References: <42b534f0-aeef-411f-80c5-db5d20c80b55@googlegroups.com> In-Reply-To: <42b534f0-aeef-411f-80c5-db5d20c80b55@googlegroups.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416006313 news.xs4all.nl 2900 [2001:888:2000:d::a6]:59888 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80792 On 2014-11-14 22:17, Richard Riehle 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. > > Thanks in advance for any suggestions, > In C, you're not creating an array of functions, but an array of _pointers_ to functions. In Python, we don't have pointers, but we do have references. For example, you can define a function: >>> def my_func(): ... print('my_func called') ... The name of the function is just a reference to that function: >>> print(my_func) You call it using (...): >>> my_func() my_func called You can store the reference in a list or bind another name to it: >>> func_by_another_name = my_func And call it by that other name: >>> func_by_another_name() my_func called Does that help?