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


Groups > comp.lang.python > #105869 > unrolled thread

List of Functions

Started byRichard Riehle <rriehle@itu.edu>
First post2016-03-27 12:38 -0700
Last post2016-03-29 08:52 +0200
Articles 20 on this page of 22 — 11 participants

Back to article view | Back to comp.lang.python


Contents

  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

Page 1 of 2  [1] 2  Next page →


#105869 — List of Functions

FromRichard Riehle <rriehle@itu.edu>
Date2016-03-27 12:38 -0700
SubjectList of Functions
Message-ID<3c44f0f8-d701-463e-bf2c-f5871c51bddf@googlegroups.com>
Several months ago, I posted a question regarding how to create a list of functions.  I quickly solved the problem on my own, but I am just now getting around to sharing my solution.  It was actually quite simple, and also quite useful for the problem I had at hand.  Below is an example of one way to do this.  There are actually some other solutions.

This solution is based on the fact that Python functions are first-class objects, and therefore enable one to emulate functional programming.   

Consider that we might need a list of buttons, each of which can behave differently based on a parameter list.   First, we define three button functions with a parameter.  Then, we create a list of those functions. 

The tricky part is how we formulate the actual function call.   I demonstrate this in the last two lines of the code below.   

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

[toc] | [next] | [standalone]


#105879

FromErik <python@lucidity.plus.com>
Date2016-03-28 00:10 +0100
Message-ID<mailman.98.1459120211.28225.python-list@python.org>
In reply to#105869
Hi Richard,

On 27/03/16 20:38, Richard Riehle wrote:
> I realize that this seems trivial to many experience Pythonistas.  But it might prove useful for those who are relative newcomers

Thanks for sharing your solution (people finding the original question 
because it happens to match their own may then find this follow-up).

However, please also read PEP8 -

https://www.python.org/dev/peps/pep-0008/

>>>> 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
>

The whitespace before the [] and () is what I'm referring you to PEP8 
about. Of course, you can do what you want - this is just a friendly 
nudge ;) That extra whitespace does make it a bit harder to grok if 
you're used to reading "typical" Python code.

BR, E.

[toc] | [prev] | [next] | [standalone]


#105882

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-03-28 01:19 +0100
Message-ID<87bn5z1mkd.fsf@bsb.me.uk>
In reply to#105869
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.

[toc] | [prev] | [next] | [standalone]


#105907

FromJussi Piitulainen <jussi.piitulainen@helsinki.fi>
Date2016-03-28 10:51 +0300
Message-ID<lf5d1qf2g7v.fsf@ling.helsinki.fi>
In reply to#105882
Ben Bacarisse writes:

> 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.

Well said.

Python should have called it "fun".

I have heard that the use of lambda for this purpose was originally not
an arbitrary choice but a typographical accident. A misinterpreted caret
or something. I also seem to remember that I've seen some discussion on
whether the story is true or not, but I forget which way it went.

[toc] | [prev] | [next] | [standalone]


#105908

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-03-28 11:58 +0300
Message-ID<87mvpjx9kh.fsf@elektro.pacujo.net>
In reply to#105907
Jussi Piitulainen <jussi.piitulainen@helsinki.fi>:

> Ben Bacarisse writes:
>> 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.
>
> Well said.
>
> Python should have called it "fun".
>
> I have heard that the use of lambda for this purpose was originally
> not an arbitrary choice but a typographical accident. A misinterpreted
> caret or something. I also seem to remember that I've seen some
> discussion on whether the story is true or not, but I forget which way
> it went.

I don't know the etymology of things, but way back when I learned
combinatory logic, the capital letter Λ was used as the name of the
abstraction algorithm.

Combinatory logic was able to express any formula without variables,
which apparently were somewhat of a sore spot for logicians in the early
20th century. So you took your formula with variables and ran it through
the Λ algorithm to get an equivalent combinator expression that didn't
have any variables.

I always thought the λ calculus arose from the abstraction algorithm.
Once the logicians had "tamed" variables with Λ, they no longer felt the
need to avoid them. You could set aside the clumsy combinators and
incorporate the lambda in the language itself.

...

As for Python, I don't feel a great need for anonymous functions.
However, I keep running into a need for anonymous classes, or, rather,
classless objects. Not a biggie. I just create a one-off inner class and
instantiate it, but I do appreciate Java's syntactic innovation.


Marko

[toc] | [prev] | [next] | [standalone]


#105911

FromDan Sommers <dan@tombstonezero.net>
Date2016-03-28 12:39 +0000
Message-ID<ndb8ln$u0f$1@dont-email.me>
In reply to#105908
On Mon, 28 Mar 2016 11:58:54 +0300, Marko Rauhamaa wrote:

> As for Python, I don't feel a great need for anonymous functions.
> However, I keep running into a need for anonymous classes, or, rather,
> classless objects. Not a biggie. I just create a one-off inner class
> and instantiate it, but I do appreciate Java's syntactic innovation.

And I always curse Java for having to create an inner class and a method
when all I need is a simple function.  :-)

I think it's Steven D'Aprano who keeps pointing out that you can always
name your tiny helper functions instead of using lambda:

    def some_complex_function():
        def f(x) = x + 2
        some_library_that_wants_a_callback(f)
        some_library_that_wants_a_callback(lambda x: x + 2)

Both calls to some_library_that_wants_a_callback run the same.

[toc] | [prev] | [next] | [standalone]


#105914

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-03-28 16:40 +0300
Message-ID<87twjqn2jn.fsf@elektro.pacujo.net>
In reply to#105911
Dan Sommers <dan@tombstonezero.net>:

> On Mon, 28 Mar 2016 11:58:54 +0300, Marko Rauhamaa wrote:
>
>> As for Python, I don't feel a great need for anonymous functions.
>> However, I keep running into a need for anonymous classes, or,
>> rather, classless objects. Not a biggie. I just create a one-off
>> inner class and instantiate it, but I do appreciate Java's syntactic
>> innovation.
>
> And I always curse Java for having to create an inner class and a
> method when all I need is a simple function. :-)
>
> I think it's Steven D'Aprano who keeps pointing out that you can
> always name your tiny helper functions instead of using lambda:
>
>     def some_complex_function():
>         def f(x) = x + 2
>         some_library_that_wants_a_callback(f)
>         some_library_that_wants_a_callback(lambda x: x + 2)
>
> Both calls to some_library_that_wants_a_callback run the same.

Yes, but I've come to realize that I quite often need more than a
function: I need an object with behavior. The solution is to use a
"helper" class.


Marko

[toc] | [prev] | [next] | [standalone]


#105926

FromChris Angelico <rosuav@gmail.com>
Date2016-03-29 08:40 +1100
Message-ID<mailman.116.1459201207.28225.python-list@python.org>
In reply to#105914
On Tue, Mar 29, 2016 at 12:40 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Dan Sommers <dan@tombstonezero.net>:
>
>> On Mon, 28 Mar 2016 11:58:54 +0300, Marko Rauhamaa wrote:
>>
>>> As for Python, I don't feel a great need for anonymous functions.
>>> However, I keep running into a need for anonymous classes, or,
>>> rather, classless objects. Not a biggie. I just create a one-off
>>> inner class and instantiate it, but I do appreciate Java's syntactic
>>> innovation.
>>
>> And I always curse Java for having to create an inner class and a
>> method when all I need is a simple function. :-)
>>
>> I think it's Steven D'Aprano who keeps pointing out that you can
>> always name your tiny helper functions instead of using lambda:
>>
>>     def some_complex_function():
>>         def f(x) = x + 2
>>         some_library_that_wants_a_callback(f)
>>         some_library_that_wants_a_callback(lambda x: x + 2)
>>
>> Both calls to some_library_that_wants_a_callback run the same.
>
> Yes, but I've come to realize that I quite often need more than a
> function: I need an object with behavior. The solution is to use a
> "helper" class.

Can you give an example of code that would benefit from a
"lambda-class" construct? Since all functions more complicated than
"return this expression" need statement syntax in Python, you'd be
pretty restricted in what you can do, so I'm thinking that maybe a
SimpleNamespace might suffice:

from types import SimpleNamespace
obj = SimpleNamespace(
    add2=lambda x: x+2,
    squared=lambda x: x*x,
)

But if "behaviour" involves mutable state, it'd be fiddly to squish
that into lambda functions, so a lambda class would be impractical
too.

What you could perhaps do is this:

def one_off(cls): return cls()

@one_off
class obj:
    def func1(self):
        ...
    def func2(self):
        ...

obj.func1()
obj.func2()

It's a statement, with all the costs and benefits thereof, but you
don't have to have "obj = obj()" at the end.

ChrisA

[toc] | [prev] | [next] | [standalone]


#105932

FromSteven D'Aprano <steve@pearwood.info>
Date2016-03-29 09:52 +1100
Message-ID<56f9b5c2$0$1606$c3e8da3$5496439d@news.astraweb.com>
In reply to#105926
On Tue, 29 Mar 2016 08:40 am, Chris Angelico wrote:

> On Tue, Mar 29, 2016 at 12:40 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
>> Dan Sommers <dan@tombstonezero.net>:
>>
>>> On Mon, 28 Mar 2016 11:58:54 +0300, Marko Rauhamaa wrote:
>>>
>>>> As for Python, I don't feel a great need for anonymous functions.
>>>> However, I keep running into a need for anonymous classes, or,
>>>> rather, classless objects. Not a biggie. I just create a one-off
>>>> inner class and instantiate it, but I do appreciate Java's syntactic
>>>> innovation.

"Classless object" is an oxymoron in Python since all values without
exception have a class. Can you explain what you mean?

Also, for the benefit of those who aren't Java coders, what do you mean
by "Java's syntactic innovation"?


>>> And I always curse Java for having to create an inner class and a
>>> method when all I need is a simple function. :-)
>>>
>>> I think it's Steven D'Aprano who keeps pointing out that you can
>>> always name your tiny helper functions instead of using lambda:
>>>
>>>     def some_complex_function():
>>>         def f(x) = x + 2
>>>         some_library_that_wants_a_callback(f)
>>>         some_library_that_wants_a_callback(lambda x: x + 2)
>>>
>>> Both calls to some_library_that_wants_a_callback run the same.
>>
>> Yes, but I've come to realize that I quite often need more than a
>> function: I need an object with behavior. The solution is to use a
>> "helper" class.
> 
> Can you give an example of code that would benefit from a
> "lambda-class" construct? 

That would be called "type" :-)

type(name, bases, namespace) returns a new class:


py> C = type("MyClass", (object,), {'foo': 1})
py> C
<class '__main__.MyClass'>
py> C.foo
1



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#105934

FromChris Angelico <rosuav@gmail.com>
Date2016-03-29 10:40 +1100
Message-ID<mailman.120.1459208421.28225.python-list@python.org>
In reply to#105932
On Tue, Mar 29, 2016 at 9:52 AM, Steven D'Aprano <steve@pearwood.info> wrote:
> That would be called "type" :-)
>
> type(name, bases, namespace) returns a new class:
>
>
> py> C = type("MyClass", (object,), {'foo': 1})
> py> C
> <class '__main__.MyClass'>
> py> C.foo
> 1

Yeah, but to do that in a single expression, you need to have all the
functions in the dictionary, so it's no improvement over
SimpleNamespace. The functions get attached to the class, not the
instance, which means they need 'self' - but without assignment, you
wouldn't be able to make much use of self anyway. Hence the call for
an example.

ChrisA

[toc] | [prev] | [next] | [standalone]


#105948

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-03-29 07:49 +0300
Message-ID<87zithx50d.fsf@elektro.pacujo.net>
In reply to#105932
Steven D'Aprano <steve@pearwood.info>:

> On Tue, 29 Mar 2016 08:40 am, Chris Angelico wrote:
>
>> On Tue, Mar 29, 2016 at 12:40 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
>>> Dan Sommers <dan@tombstonezero.net>:
>>>
>>>> On Mon, 28 Mar 2016 11:58:54 +0300, Marko Rauhamaa wrote:
>>>>> As for Python, I don't feel a great need for anonymous functions.
>>>>> However, I keep running into a need for anonymous classes, or,
>>>>> rather, classless objects. Not a biggie. I just create a one-off
>>>>> inner class and instantiate it, but I do appreciate Java's
>>>>> syntactic innovation.
>
> "Classless object" is an oxymoron in Python since all values without
> exception have a class. Can you explain what you mean?

In class terms, I mean a closure class that has precisely one instance.
Just like a closure function has precisely one instance.

In object terms, I mean an object that implements an interface but whose
class is not of essence.

> Also, for the benefit of those who aren't Java coders, what do you mean
> by "Java's syntactic innovation"?

        HelloWorld frenchGreeting = new HelloWorld() {
            String name = "tout le monde";
            public void greet() {
                greetSomeone("tout le monde");
            }
            public void greetSomeone(String someone) {
                name = someone;
                System.out.println("Salut " + name);
            }
        };

        <URL: https://docs.oracle.com/javase/tutorial/java/javaOO/anonym
        ousclasses.html>


Marko

[toc] | [prev] | [next] | [standalone]


#105947

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-03-29 07:45 +0300
Message-ID<874mbpyjrf.fsf@elektro.pacujo.net>
In reply to#105926
Chris Angelico <rosuav@gmail.com>:

> On Tue, Mar 29, 2016 at 12:40 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
>> Dan Sommers <dan@tombstonezero.net>:
>>
>>> On Mon, 28 Mar 2016 11:58:54 +0300, Marko Rauhamaa wrote:
>>>
>>>> As for Python, I don't feel a great need for anonymous functions.
>>>> However, I keep running into a need for anonymous classes, or,
>>>> rather, classless objects. Not a biggie. I just create a one-off
>>>> inner class and instantiate it, but I do appreciate Java's syntactic
>>>> innovation.
>>>
>>> And I always curse Java for having to create an inner class and a
>>> method when all I need is a simple function. :-)
>>>
>>> I think it's Steven D'Aprano who keeps pointing out that you can
>>> always name your tiny helper functions instead of using lambda:
>>>
>>>     def some_complex_function():
>>>         def f(x) = x + 2
>>>         some_library_that_wants_a_callback(f)
>>>         some_library_that_wants_a_callback(lambda x: x + 2)
>>>
>>> Both calls to some_library_that_wants_a_callback run the same.
>>
>> Yes, but I've come to realize that I quite often need more than a
>> function: I need an object with behavior. The solution is to use a
>> "helper" class.
>
> Can you give an example of code that would benefit from a
> "lambda-class" construct?

Here's a recent example:

========================================================================
            def process(self, source):
                awk = self
                class Source(snake.FieldSource):
                    def emit_fields(self):
                        for fields in snake.get_fields(
                                source, awk.encoding, awk.errors,
                                awk.eol, awk.delim):
                            if awk.selector is None or awk.selector(fields):
                                yield [ fields[i] for i in awk.columns ]
                return Source(self.encoding, self.errors, self.eol, self.delim)
========================================================================

Here's another, older one:

========================================================================
    def __init__(self, server, sock, domain, peer):
        super().__init__(server, sock)
        conn = self
        client_ip = peer[0]

        [...]

        class SPF_HELO(STATE):
            def terminate(self):
                conn.resume()
                conn.spf_query.cancel()
                conn.close()
                if conn.timer is not None:
                    conn.timer.cancel()
                    conn.timer = None
                conn.set_state(ZOMBIE)

            def handle_spf_verdict(self, verdict, reason, *capabilities):
                conn.resume()
                conn.log("verdict {} reason {}".format(verdict, reason))
                # RFC 4408 §2.5.5 calls for leniency for SoftFail
                #if verdict in [ SPFClient.FAIL, SPFClient.SOFT_FAIL ]:
                if verdict in [ SPFClient.FAIL ]:
                    conn.respond(550, "SPF check failed: {}".format(reason))
                    conn.set_state(IDLE)
                    return
                conn.respond(250, server.domain, *capabilities)
                conn.set_state(IDLE)
========================================================================


Marko

[toc] | [prev] | [next] | [standalone]


#105949

FromChris Angelico <rosuav@gmail.com>
Date2016-03-29 16:00 +1100
Message-ID<mailman.129.1459227615.28225.python-list@python.org>
In reply to#105947
On Tue, Mar 29, 2016 at 3:45 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Chris Angelico <rosuav@gmail.com>:
>
>> On Tue, Mar 29, 2016 at 12:40 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
>>> Dan Sommers <dan@tombstonezero.net>:
>>>
>>>> On Mon, 28 Mar 2016 11:58:54 +0300, Marko Rauhamaa wrote:
>>>>
>>>>> As for Python, I don't feel a great need for anonymous functions.
>>>>> However, I keep running into a need for anonymous classes, or,
>>>>> rather, classless objects. Not a biggie. I just create a one-off
>>>>> inner class and instantiate it, but I do appreciate Java's syntactic
>>>>> innovation.
>>>>
>>>> And I always curse Java for having to create an inner class and a
>>>> method when all I need is a simple function. :-)
>>>>
>>>> I think it's Steven D'Aprano who keeps pointing out that you can
>>>> always name your tiny helper functions instead of using lambda:
>>>>
>>>>     def some_complex_function():
>>>>         def f(x) = x + 2
>>>>         some_library_that_wants_a_callback(f)
>>>>         some_library_that_wants_a_callback(lambda x: x + 2)
>>>>
>>>> Both calls to some_library_that_wants_a_callback run the same.
>>>
>>> Yes, but I've come to realize that I quite often need more than a
>>> function: I need an object with behavior. The solution is to use a
>>> "helper" class.
>>
>> Can you give an example of code that would benefit from a
>> "lambda-class" construct?
>
> Here's a recent example:
>
> ========================================================================
>             def process(self, source):
>                 awk = self
>                 class Source(snake.FieldSource):
>                     def emit_fields(self):
>                         for fields in snake.get_fields(
>                                 source, awk.encoding, awk.errors,
>                                 awk.eol, awk.delim):
>                             if awk.selector is None or awk.selector(fields):
>                                 yield [ fields[i] for i in awk.columns ]
>                 return Source(self.encoding, self.errors, self.eol, self.delim)
> ========================================================================
>
> Here's another, older one:
>
> ========================================================================
>     def __init__(self, server, sock, domain, peer):
>         super().__init__(server, sock)
>         conn = self
>         client_ip = peer[0]
>
>         [...]
>
>         class SPF_HELO(STATE):
>             def terminate(self):
>                 conn.resume()
>                 conn.spf_query.cancel()
>                 conn.close()
>                 if conn.timer is not None:
>                     conn.timer.cancel()
>                     conn.timer = None
>                 conn.set_state(ZOMBIE)
>
>             def handle_spf_verdict(self, verdict, reason, *capabilities):
>                 conn.resume()
>                 conn.log("verdict {} reason {}".format(verdict, reason))
>                 # RFC 4408 §2.5.5 calls for leniency for SoftFail
>                 #if verdict in [ SPFClient.FAIL, SPFClient.SOFT_FAIL ]:
>                 if verdict in [ SPFClient.FAIL ]:
>                     conn.respond(550, "SPF check failed: {}".format(reason))
>                     conn.set_state(IDLE)
>                     return
>                 conn.respond(250, server.domain, *capabilities)
>                 conn.set_state(IDLE)
> ========================================================================

Since those functions have statements in them, there's not going to be
any way to construct this class as an expression (short of stupid
messing around with exec, which would be a bad idea). So the best
you're likely to be able to do is a metaclass or decorator that
short-hands the instantiation - or crafting your other code such that
it takes the class, rather than an instance of the class. The latter
is quite Pythonic, but generally not as well regarded in other
languages, sometimes for obvious reasons like "classes aren't first
class objects". :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#105935

FromSteven D'Aprano <steve@pearwood.info>
Date2016-03-29 10:40 +1100
Message-ID<56f9c104$0$1604$c3e8da3$5496439d@news.astraweb.com>
In reply to#105907
On Mon, 28 Mar 2016 06:51 pm, Jussi Piitulainen wrote:

> Ben Bacarisse writes:
> 
>> 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.

I'm not sure that "lambda" is any more mysterious or meaningless than other
terms used in computing. What's a closure? A trampoline? A future? Mapping?
Thread? Greenlet? Mantissa? Not to mention terms from mathematics that
people simply memorise, like "sin", "cos", "power".

Not to mention "Monad". I don't think *anyone* knows what a Monad is ;-)


> Well said.
> 
> Python should have called it "fun".

As in the 80s pop hit by Cyndia Lauper, "Girls Just Wanna Have Anonymous
Functions"?


> I have heard that the use of lambda for this purpose was originally not
> an arbitrary choice but a typographical accident. A misinterpreted caret
> or something. I also seem to remember that I've seen some discussion on
> whether the story is true or not, but I forget which way it went.

I don't know whether or not it is true, but I've heard the same thing.
Quote:

(Note: it may seem perverse to use lambda to introduce a procedure/function.
The notation goes back to Alonzo Church, who in the 1930's started with
a "hat" symbol; he wrote the square function as "ŷ . y × y". But frustrated
typographers moved the hat to the left of the parameter and changed it to a
capital lambda: "Λy . y × y"; from there the capital lambda was changed to
lowercase, and now we see "λy . y × y" in math books and (lambda (y) (* y
y)) in Lisp. If it were up to me, I'd use fun or maybe ^. ) 

http://norvig.com/lispy2.html



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#105936

FromRandom832 <random832@fastmail.com>
Date2016-03-28 19:50 -0400
Message-ID<mailman.121.1459209021.28225.python-list@python.org>
In reply to#105935
On Mon, Mar 28, 2016, at 19:40, Steven D'Aprano wrote:
> Not to mention "Monad". I don't think *anyone* knows what a Monad is ;-)

A monad is just a monoid in the category of endofunctors; what's the
problem?

Well, someone had to say it.

[toc] | [prev] | [next] | [standalone]


#105937

FromChris Angelico <rosuav@gmail.com>
Date2016-03-29 10:54 +1100
Message-ID<mailman.122.1459209302.28225.python-list@python.org>
In reply to#105935
On Tue, Mar 29, 2016 at 10:50 AM, Random832 <random832@fastmail.com> wrote:
> On Mon, Mar 28, 2016, at 19:40, Steven D'Aprano wrote:
>> Not to mention "Monad". I don't think *anyone* knows what a Monad is ;-)
>
> A monad is just a monoid in the category of endofunctors; what's the
> problem?
>
> Well, someone had to say it.

A monad is what you get in front of every Twitch stream. A polyad is
what you get on dodgy torrent sites. Of course, only functional
languages have them; nonfunctional languages use adblockers.

ChrisA

[toc] | [prev] | [next] | [standalone]


#105941

FromRustom Mody <rustompmody@gmail.com>
Date2016-03-28 19:23 -0700
Message-ID<c8915413-0e9a-4dc0-a041-13fe2ce27ba2@googlegroups.com>
In reply to#105935
On Tuesday, March 29, 2016 at 5:11:02 AM UTC+5:30, Steven D'Aprano wrote:
> On Mon, 28 Mar 2016 06:51 pm, Jussi Piitulainen wrote:
> 
> > Ben Bacarisse writes:
> > 
> >> 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.
> 
> I'm not sure that "lambda" is any more mysterious or meaningless than other
> terms used in computing. What's a closure? A trampoline? A future? Mapping?
> Thread? Greenlet? Mantissa? Not to mention terms from mathematics that
> people simply memorise, like "sin", "cos", "power".

Its my conjecture/contention that people find λ hard because 98% of material
presenting it is backward: "A λ is an 'anonymous function"

How does "A constant is an anonymous variable"   sound?

[toc] | [prev] | [next] | [standalone]


#105942

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-03-29 03:45 +0100
Message-ID<87y492xaqh.fsf@bsb.me.uk>
In reply to#105935
Steven D'Aprano <steve@pearwood.info> writes:

> On Mon, 28 Mar 2016 06:51 pm, Jussi Piitulainen wrote:
>
>> Ben Bacarisse writes:
>> 
>>> 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.
>
> I'm not sure that "lambda" is any more mysterious or meaningless than other
> terms used in computing. What's a closure? A trampoline? A future? Mapping?
> Thread? Greenlet? Mantissa? Not to mention terms from mathematics that
> people simply memorise, like "sin", "cos", "power".

Well lambda is more arbitrary than those that include helpful hints to
the technical meaning in the plain English meaning.  What's more, though
they express important concepts they are not all part of Python's
syntax.  Lambda has no helpful meaning and yet has to appear in the
program's text.

Anyway, even it is were exactly like all the other examples, is that a
reason to have more?  I'd argue that we should have as few such words as
possible, especially in the syntax.

If functions were defined

  fun f(x): return x * x

then an anonymous function could be written as a function definition but
the name

  fun (x): return x * x

I suppose you could do that even with "def" but it's a bit less
mnemonic.

There's probably a good reason why this was not done -- maybe it stops
the grammar being LL(1)?  And if lambda's arrived late, and a new
keyword is needed to flag a function expression, then lambda is
appealing to the designer since it's less likely to be in use as an
identifier in existing code than many more meaningful keywords.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#105943

FromChris Angelico <rosuav@gmail.com>
Date2016-03-29 14:33 +1100
Message-ID<mailman.125.1459222396.28225.python-list@python.org>
In reply to#105942
On Tue, Mar 29, 2016 at 1:45 PM, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> If functions were defined
>
>   fun f(x): return x * x
>
> then an anonymous function could be written as a function definition but
> the name
>
>   fun (x): return x * x
>
> I suppose you could do that even with "def" but it's a bit less
> mnemonic.
>
> There's probably a good reason why this was not done -- maybe it stops
> the grammar being LL(1)?  And if lambda's arrived late, and a new
> keyword is needed to flag a function expression, then lambda is
> appealing to the designer since it's less likely to be in use as an
> identifier in existing code than many more meaningful keywords.

Some languages are exactly like this. I'm not sure what the reason is
for Python's difference, but one possibility is the mess that comes
from having statements inside expressions when indentation is
significant to statements (but free-form in expressions).

ChrisA

[toc] | [prev] | [next] | [standalone]


#105951

FromRustom Mody <rustompmody@gmail.com>
Date2016-03-28 23:21 -0700
Message-ID<10836e10-316a-492d-98ff-1928ac7eb8e6@googlegroups.com>
In reply to#105942
On Tuesday, March 29, 2016 at 8:16:12 AM UTC+5:30, Ben Bacarisse wrote:
> Steven D'Aprano writes:
> 
> > On Mon, 28 Mar 2016 06:51 pm, Jussi Piitulainen wrote:
> >
> >> Ben Bacarisse writes:
> >> 
> >>> 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.
> >
> > I'm not sure that "lambda" is any more mysterious or meaningless than other
> > terms used in computing. What's a closure? A trampoline? A future? Mapping?
> > Thread? Greenlet? Mantissa? Not to mention terms from mathematics that
> > people simply memorise, like "sin", "cos", "power".
> 
> Well lambda is more arbitrary than those that include helpful hints to
> the technical meaning in the plain English meaning.  What's more, though
> they express important concepts they are not all part of Python's
> syntax.  Lambda has no helpful meaning and yet has to appear in the
> program's text.

I am guessing its a long time since you were introduced to computers.
And you've forgotten what the jargon first felt like.

As a student I remember my friends who were doing projects with me
working at my place.
And my mum made the strange remark: "You guys use all the words that I know.
And you make them into sentences that have no meaning at all."

Since you've 'forgotten' that jargonification stage, lets start with 'memory'.
'Memory' is a very old English word, eg. what I did right now -- remembering 
what my Mum told me some 35 years ago: Does it have anything to do with what
memory means in computer-jargon?

Dijkstra liked to point out that CS was backward in America compared to Europe
because in Europe they used 'store' but Americans used anthropomorphism like memory

Now given that store can mean -- among other things -- 
- room where I dump stuff
- shop where I buy bread and eggs
- etc
why is Dijkstra's preferred use actually any better?

Or his most famous request: When you make an error please call it an error.
There is no 'mean little bug' crawling out when you were not looking.

Shall we rename 'debugging programs' to 'error-correcting code(s)'?

Likewise
computer has always been a mathematician that computes
and
program is for things like radio-program, concert-program etc

> 
> Anyway, even it is were exactly like all the other examples, is that a
> reason to have more?  I'd argue that we should have as few such words as
> possible, especially in the syntax.

Its a hard question
Take an old word and give it a new related but different meaning
vs
Invent a nonsense word for a genuinely new concept

My own finding is that repurposing old words to new concepts causes more
confusion and misunderstanding than understanding and 'progress'

A record of how sticking to sloppy terminology perpetuates sloppy understanding:

http://blog.languager.org/2011/02/cs-education-is-fat-and-weak-2.html

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web