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


Groups > comp.lang.python > #80792

Re: Array of Functions

Date 2014-11-14 23:02 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: Array of Functions
References <42b534f0-aeef-411f-80c5-db5d20c80b55@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.15833.1416006313.18130.python-list@python.org> (permalink)

Show all headers | View raw


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)
<function my_func at 0x0000000002CBCBF8>

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?

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