Syntax for one-line "nonymous" functions in "declaration style"

Alexandre Brault abrault at mapgears.com
Wed Mar 27 12:41:16 EDT 2019


On 2019-03-27 10:42 a.m., Paul Moore wrote:
> On Wed, 27 Mar 2019 at 12:27, Alexey Muranov <alexey.muranov at gmail.com> wrote:
>> On mer., mars 27, 2019 at 10:10 AM, Paul Moore <p.f.moore at gmail.com>
>> wrote:
>>> On Wed, 27 Mar 2019 at 08:25, Alexey Muranov
>>> <alexey.muranov at gmail.com> wrote:
>>>>  Whey you need a simple function in Python, there is a choice
>>>> between a
>>>>  normal function declaration and an assignment of a anonymous
>>>> function
>>>>  (defined by a lambda-expression) to a variable:
>>>>
>>>>      def f(x): return x*x
>>>>
>>>>  or
>>>>
>>>>      f = lambda x: x*x
>>>>
>>>>  It would be however more convenient to be able to write instead just
>>>>
>>>>      f(x) = x*x
>>> Why? Is saving a few characters really that helpful? So much so that
>>> it's worth adding a *third* method of defining functions, which would
>>> need documenting, adding to training materials, etc, etc?
>> Because i think i would prefer to write it this way.
> That's not likely to be sufficient reason for changing a language
> that's used by literally millions of people.
>
>> (Almost no new documentation or tutorials would be needed IMHO.)
> Documentation would be needed to explain how the new construct worked,
> for people who either wanted to use it or encountered it in other
> people's code. While it may be obvious to you how it works, it likely
> won't be to others, and there will probably be edge cases you haven't
> considered that others will find and ask about.

For what it's worth, if I encountered "f(x) = x * x" in code, my first
thought would be that Python somehow added a way to return an assignable
reference from a function, rather than this being an anonymous function
declaration.

So documentation of that syntax would 100% be required

Alex




More information about the Python-list mailing list