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


Groups > comp.lang.python > #105882

Re: List of Functions

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.python
Subject Re: List of Functions
Date 2016-03-28 01:19 +0100
Organization A noiseless patient Spider
Message-ID <87bn5z1mkd.fsf@bsb.me.uk> (permalink)
References <3c44f0f8-d701-463e-bf2c-f5871c51bddf@googlegroups.com>

Show all headers | View raw


Richard Riehle <rriehle@itu.edu> writes:

> Several months ago, I posted a question regarding how to create a list
> of functions.
<snip>
> I realize that this seems trivial to many experience Pythonistas.  But
> it might prove useful for those who are relative newcomers to the
> language.  In any case, I hope someone can find it helpful.
>
>>>> def button1(number):
> 	print ('button1 = ', number)  ## define the buttons
>>>> def button2(number):
> 	print ('button2 = ', number)
>>>> def button3(number):
> 	print ('button3 = ', number)	
>>>> buttonList = [button1, button2, button3]  ## create the list
>>>>
>>>> buttonList [1] (25)          ## using positional association
> button2 =  25                    
>>>>buttonList [0] (number = 78)  ## using named association
> button1 = 78

Anywhere you see a pattern there is the opportunity to make a function
that captures the pattern.  You could choose to have a button-function
making function like this:

  def makeButton(n):
      return lambda number: print('button%d = %d' % (n, number))

It's shame that anonymous functions (for that's what's being returned
here -- a function with no name) were born of a subject that used
arbitrary Greek letters for things.  We seem stuck with the mysterious
but meaningless "lambda" for a very simple and useful idea.  So maybe
it's better to do it with a named local function instead:

  def makeButton(n):
      def button(number): print('button%d = %d' % (n, number))
      return button;

And now you can use code to make the list since the button number is now
data.

  buttonList = [makeButton(i) for i in range(1, 3)]

-- 
Ben.

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


Thread

List of Functions Richard Riehle <rriehle@itu.edu> - 2016-03-27 12:38 -0700
  Re: List of Functions Erik <python@lucidity.plus.com> - 2016-03-28 00:10 +0100
  Re: List of Functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-28 01:19 +0100
    Re: List of Functions Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-28 10:51 +0300
      Re: List of Functions Marko Rauhamaa <marko@pacujo.net> - 2016-03-28 11:58 +0300
        Re: List of Functions Dan Sommers <dan@tombstonezero.net> - 2016-03-28 12:39 +0000
          Re: List of Functions Marko Rauhamaa <marko@pacujo.net> - 2016-03-28 16:40 +0300
            Re: List of Functions Chris Angelico <rosuav@gmail.com> - 2016-03-29 08:40 +1100
              Re: List of Functions Steven D'Aprano <steve@pearwood.info> - 2016-03-29 09:52 +1100
                Re: List of Functions Chris Angelico <rosuav@gmail.com> - 2016-03-29 10:40 +1100
                Re: List of Functions Marko Rauhamaa <marko@pacujo.net> - 2016-03-29 07:49 +0300
              Re: List of Functions Marko Rauhamaa <marko@pacujo.net> - 2016-03-29 07:45 +0300
                Re: List of Functions Chris Angelico <rosuav@gmail.com> - 2016-03-29 16:00 +1100
      Re: List of Functions Steven D'Aprano <steve@pearwood.info> - 2016-03-29 10:40 +1100
        Re: List of Functions Random832 <random832@fastmail.com> - 2016-03-28 19:50 -0400
        Re: List of Functions Chris Angelico <rosuav@gmail.com> - 2016-03-29 10:54 +1100
        Re: List of Functions Rustom Mody <rustompmody@gmail.com> - 2016-03-28 19:23 -0700
        Re: List of Functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-29 03:45 +0100
          Re: List of Functions Chris Angelico <rosuav@gmail.com> - 2016-03-29 14:33 +1100
          Re: List of Functions Rustom Mody <rustompmody@gmail.com> - 2016-03-28 23:21 -0700
            Re: List of Functions Marko Rauhamaa <marko@pacujo.net> - 2016-03-29 09:50 +0300
            Re: List of Functions Christian Gollwitzer <auriocus@gmx.de> - 2016-03-29 08:52 +0200

csiph-web