[Python-ideas] Fwd: Make parenthesis optional in parameterless functions definitions
Stefan Krah
stefan at bytereef.org
Thu Mar 31 14:17:04 EDT 2016
Mahan Marwat <mahanmarwat at ...> writes:
> I have an idea of making parenthesis optional for functions having no
parameters. i.e
>
> def greet: # note the missing parenthesis
> print('hello')
>
This is an interesting idea, but it does not play well with the concept of
function evaluation and higher order functions.
Evaluation of functions is triggered when they're applied to something:
>>> def greet():
... print("hello")
...
>>> lst = [greet, greet]
>>> for f in lst:
... f() # function application
...
hello
hello
If a function does not take any arguments, it must be eagerly evaluated
when it is stored in a list.
There is an exception: OCaml allows argument-less *methods*:
let greeterInTheKingdomOfNouns = object
method greet = print_string "hello\n"
end;;
val greeterInTheKingdomOfNouns : < greet : unit > = <obj>
# greeterInTheKingdomOfNouns#greet;;
hello
- : unit = ()
This is different though: Here one sends the message "greet" to the
object "greeterInTheKingdomOfNouns", so function application is replaced
by message passing.
In short, these argument-less functions are amusing but not so useful
in practice.
Stefan Krah
More information about the Python-ideas
mailing list