[Python-ideas] Enhance definition of functions

Göktuğ Kayaalp goktug.kayaalp at gmail.com
Thu Aug 1 05:24:21 CEST 2013


I believe that anything that can not be expressed as a Python anonymous
function must be a def.  It is possible to express conditionals and
loops within a lambda statement, if that is what you are looking for:

>>> hidden = [ file for file in os.listdir(givenpath) if file.startswith(".")] \
..: if isdir(givenpath) \
..: else [givenpath.startswith(".")

BTW, if a multi-statement anonymous function syntax was to be considered
seriously, I'd recommend a lambda statement with colon replaced with a
brace-delimited block, which would barely cause code written for an
interpreter lacking it get refused:

server = nodeishServer(
    lambda req, res {
        res.writeHead(200, ContentType= "text/html");
        res.end("Hello");
    }
)

In fact, grammar for every statement which introduces a new block (if,
def, for, with, lambda) can be altered such that if the statement ends
with a `:' (semicolon), following lines are parsed as in usual Python
syntax, or, if the statement ends with a `{' (left brace), following
lines are parsed with non-indentation defined, C-ish syntax. So:

def fibonacci(n):
    x, y, z = 1, 1, 0
    for i in range(1, n):
        z = x
        x += y
        y = x
    return x

could be also written as

def fibonacci(n) {
    x, y, z = 1, 1, 0;
    for i in range(1, n) {
        z = x;
        x += y;
        y = x;
    }
    return x;
}

which is a) subject of a different thread, and b) ridiculous.

-gk

On 30 July 2013 18:19, Musical Notation <musicdenotation at gmail.com> wrote:
> Yes, I know that multiline lambda will never be implemented in Python, but
> in many languages it is possible to write an anonymous function without
> using lambda at all.
> In JavaScript:
> Instead of "function <name>(<variables>){code}" you can write "var name;
> name=function(<variables>){code}"
> Python (proposed):
> def func(a,b):
>     print(a+b)
>     return a+b
>
> becomes
>
> func=function a,b:
>     print(a+b)
>     return a+b
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>


More information about the Python-ideas mailing list