[Python-ideas] Re: Define functions without parentheses (if no parameters given)

2021-06-11 Thread Guido van Rossum
On Fri, Jun 11, 2021 at 7:47 AM Jelle Zijlstra 
wrote:

>
> El jue, 10 jun 2021 a las 19:30, Cameron Simpson ()
> escribió:
>
>> On 11Jun2021 10:01, Cameron Simpson  wrote:
>>
>> It also struck me: functions with _no_ parameters are pretty rare.
>>
>> [...]
>>
> I got curious so I checked a large codebase I have access to. Out of 85540
> functions, 8244 take no arguments. Many of them are test functions.
>

I'm sure the idea is dead now, but it reminds me of an amusing anecdote I
heard in college.

You may not have heard of a language named Algol-68, but it was a big thing
when I learned programming in Amsterdam in the late '70s. Like its
predecessor, Algol-60, it had a feature where parameter-less functions
could both be *defined* and *called* without parentheses.

Now, in Algol-60, this was easy, because there were no function pointers,
so whenever the compiler saw the name of a parameter-less function, it
would generate the code to call it. (Example: "x := random*2".) But
Algol-68 *did* have function pointers, so the language design committee had
to introduce a complicated wrinkle into the type system so that you could
write things like "f := random" (interpreting it as a function pointer) but
also "x := 2*random" (calling it). The distinguishing property was the type
of the receiving end (i.e., f had to be declared as a parameterless
function pointer, and x had to be a number).

So one of the classes I took near the end of my studies was taught by the
lead author of Algol-68 (Adriaan van Wijngaarden). He was well respected
and near retirement age, so he could do what he wanted in class, and
sometimes he just told anecdotes from the past (I'd love such a job :-).
One day the issue with parameter-less functions came up, and by then a new
language named C had gained some notoriety. C, of course, has function
pointers and parameter-less functions, but both the declaration and the
call must use an empty pair of parentheses (i.e, "x = 2*random()", like
Python). Van Wijngaarden told us, somewhat ruefully, that, had the design
committee known that programmers would be happy to write that empty pair of
parentheses, they would have been able to simplify a significant corner of
Algol-68's type system.

This was one of the seminal ideas that went into Python's design.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DQGLY3ZLFMGVOC4JE7E2TLMWDROK2PAE/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Define functions without parentheses (if no parameters given)

2021-06-11 Thread Jelle Zijlstra
El jue, 10 jun 2021 a las 19:30, Cameron Simpson () escribió:

> On 11Jun2021 10:01, Cameron Simpson  wrote:
> >So your idea does not suck. But it may not motivate anyone to implement
> >it, or even to agreed that it should be implemented.
>
> It also struck me: functions with _no_ parameters are pretty rare.
>
> I had a glance through my own code and aside from some closures
> (functions within a running function, getting their variables from the
> enclosing scope) I've got a few which either access state from some
> global or which generate something standalone, eg a primes() function
> which just generates the primes starting from 2, a function I maybe even
> don't use. They are very few.
>
I got curious so I checked a large codebase I have access to. Out of 85540
functions, 8244 take no arguments. Many of them are test functions.


>
> So these seem pretty rare. Just how often do you write such a function
> yourself? Got a real world example?
>
> I'm making an argument that this is already a pretty niche situation
> here.
>
> Cheers,
> Cameron Simpson 
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/UXDUUDLJT4JASMBOM7TJXL36MGD3BYI2/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OX73FEXG5LOGGMBWBIEVU443P55Y3VBA/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Define functions without parentheses (if no parameters given)

2021-06-11 Thread Johnathan Irvin
non sequitur

Route functions as seen in flask or fastapi.

These functions are often decorated by a route, and may not apply here but
are often found with routes that return a page that doesn't take parameters
such as a home page or a contact page.

On Thu, Jun 10, 2021, 10:30 PM Cameron Simpson  wrote:

> On 11Jun2021 10:01, Cameron Simpson  wrote:
> >So your idea does not suck. But it may not motivate anyone to implement
> >it, or even to agreed that it should be implemented.
>
> It also struck me: functions with _no_ parameters are pretty rare.
>
> I had a glance through my own code and aside from some closures
> (functions within a running function, getting their variables from the
> enclosing scope) I've got a few which either access state from some
> global or which generate something standalone, eg a primes() function
> which just generates the primes starting from 2, a function I maybe even
> don't use. They are very few.
>
> So these seem pretty rare. Just how often do you write such a function
> yourself? Got a real world example?
>
> I'm making an argument that this is already a pretty niche situation
> here.
>
> Cheers,
> Cameron Simpson 
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/UXDUUDLJT4JASMBOM7TJXL36MGD3BYI2/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DNDJHT3BYQGFV6ZF56U5KGQQVKXBBZVM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Define functions without parentheses (if no parameters given)

2021-06-11 Thread Steven D'Aprano
On Fri, Jun 11, 2021 at 10:01:06AM +1000, Cameron Simpson wrote:

> Another thing to keep in mind with any syntax suggestion (not that it 
> applies well here, because really, what else can your suggestion mean?) 
> it that every addition syntax is a detour into the unused space of 
> possible token paths.

I'm not arguing for or against it, but parenthesis-free definitions 
"could" mean something rather different, such as (let's say) a function 
that uses dynamic scoping instead of lexical, and always gets its 
arguments from the calling scope.

def func:
return min(a, 10)

# first caller
a = 5
print(func())  # --> 5

# second caller
def my_function():
   a = 20
   print(func())
my_function()  # --> 10


If I wanted dynamic scopes, I wouldn't design the API that way. But we 
could.

Another possibility would be computed values (sort of like properties):

def now:
return strftime('%H:%M:%S')

print(now)  # --> 21:23:20
time.sleep(15)
print(now)  # --> 21:23:35

The point here is to agree with Cameron that every time we choose to use 
syntax for one thing, that precludes us from using that same syntax for 
a different thing.


-- 
Steve
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HJAFAC4XCHNMH3KFQLPPWX5AVQXHYCCI/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Define functions without parentheses (if no parameters given)

2021-06-10 Thread Cameron Simpson
On 10Jun2021 22:57, Johnathan Irvin  wrote:
>non sequitur

Au contraire! Seems relevant to me.

>Route functions as seen in flask or fastapi.
>
>These functions are often decorated by a route, and may not apply here but
>are often found with routes that return a page that doesn't take parameters
>such as a home page or a contact page.

Ah, good example.

Thanks,
Cameron Simpson 
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2GDB3YQD6OXCZDYG6GIBLRAYO3OKJAGY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Define functions without parentheses (if no parameters given)

2021-06-10 Thread Cameron Simpson
On 11Jun2021 10:01, Cameron Simpson  wrote:
>So your idea does not suck. But it may not motivate anyone to implement
>it, or even to agreed that it should be implemented.

It also struck me: functions with _no_ parameters are pretty rare.

I had a glance through my own code and aside from some closures 
(functions within a running function, getting their variables from the 
enclosing scope) I've got a few which either access state from some 
global or which generate something standalone, eg a primes() function 
which just generates the primes starting from 2, a function I maybe even 
don't use. They are very few.

So these seem pretty rare. Just how often do you write such a function 
yourself? Got a real world example?

I'm making an argument that this is already a pretty niche situation 
here.

Cheers,
Cameron Simpson 
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UXDUUDLJT4JASMBOM7TJXL36MGD3BYI2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Define functions without parentheses (if no parameters given)

2021-06-10 Thread Cameron Simpson
On 10Jun2021 23:07, Boštjan Mejak  wrote:
>Thank you, Steve, for your answer. You're absolutely right. But I needed an 
>answer to clarify that my idea, well, sucks.
>
>I was just relating to class definitions -- being able to do 'class 
>MyClass: pass' -- and had an idea about having this same 
>non-parenthesized version in case of no given parameters in a function 
>definition.

It doesn't suck. Consider, for example, a lambda with no parameters:

random16 = lambda: random.randint(1, 16)

Stephen is saying it isn't a gleaming nugget of gold that wants, nay, 
_demands_ to be implelemented. It does really only save a couple of 
characters and a little visual noise.

Another thing to keep in mind with any syntax suggestion (not that it 
applies well here, because really, what else can your suggestion mean?) 
it that every addition syntax is a detour into the unused space of 
possible token paths. This is because the exsiting grammar defines a 
space of _used_ token paths with existing meanings. Allowing a new path 
of previously illegal tokens (==> unused and therefore available) should 
bring a specific benefit, as it _precludes_ that path/syntax from being 
used for anything else.

Python tends to be pretty conservative here: if what you want to write 
this way can _already_ be written another way which is not really very 
worse, the benefit the new way brings is small.

In a sense this is a similar criterion to the oft cited view that "not 
everything needs its own function", usually applied to advocating for 
small one/few line functions to be added to the standard library.

So your idea does not suck. But it may not motivate anyone to implement 
it, or even to agreed that it should be implemented.

Cheers,
Cameron Simpson 
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QWLF6YT64MMJUA7YU4W72XXR3K4HT4V2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Define functions without parentheses (if no parameters given)

2021-06-10 Thread Boštjan Mejak
Thank you, Steve, for your answer. You're absolutely right. But I needed an 
answer to clarify that my idea, well, sucks.

I was just relating to class definitions -- being able to do 'class MyClass: 
pass' -- and had an idea about having this same non-parenthesized version in 
case of no given parameters in a function definition.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XBYWV27KZQFZBHILZXFE4RROAOV4ZJ74/
Code of Conduct: http://python.org/psf/codeofconduct/