DEV Community

Discussion on: Python 4 - New Function Syntax, Maybe?

Collapse
 
miniscruff profile image
miniscruff

Hey Mike, I enjoy these sort of thought experiments and exercises on what a language could look like. As much as I love using anonymous functions in javascript I never really felt like they were very optimal. And often times I find myself getting lost in my own code with the lack of coherent names. I think that pythons style of having named functions is, although maybe a bit jumpy at times, more readable.

I would like to compare your example with a current python version.

result = map(range(60), (def (number):
  if number % 2 == 0:
    return "even"
  elif number % 2 == 1:
    return "odd"
  else:
    return "whoa, freaky number"
))

and my own...

def number_type(number):
    if number % 2 == 0:
        return 'even'
    if number % 2 == 1:
        return 'odd'
    return 'woah, freaky number'


result = map(number_type, range(60))

In your example of using an anonymous function, you must read at least a portion of it to understand what is going on. Whereas in my example, we have given it a name. It may not be the best name but it is a description nonetheless. Now to understand what result is I can simply read the one line and realize that it is a map of the number types from 0-60.

I also find your first example of python a little bit strange. Given that a similar API to fetch in python could be considered requests I think it would look a little closer to this:

import requests
req = requests.get('http://example.com/movies.json')
print(req.json)

If this intent of having inline functions was to allow for more async style callbacks then simply using async and await would do the trick as well.

Finally to your last point, I believe, all python variables in functions are already scoped. And if you wanted to create a function and delete it afterward you can literally del it.

def fib(n):
  a, b = 0, 1
  while a < n:
    yield a
    a, b = b, a+b
result = fib(60)
print(a, b) # a and b are undefined
del fib # fib is now undefined

You could probably wrap this in a decorator or a context manager for single use function, but I am not sure what benefit it will have.

Anyway, sorry if this was too critical it was, of course, a thought experiment.

Collapse
 
ablablalbalblab profile image
a

Hey miniscruff,

I understand that for some, the syntax may not be as practical and intuitive. That's why I believe it will never be implemented at all :)