Re: Why does not Python accept functions with no names?

2022-02-21 Thread Abdur-Rahmaan Janhangeer
> BTW, this is not what is usually meant by the term "anonymous
function". An anonymous function is one that is not bound
to *any* name. The thing you're proposing wouldn't be
anonymous -- it would have a name, that name being the empty
string.


Thanks for clarifying this point 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-21 Thread Avi Gross via Python-list
Eric,
You bring up a related topic which I agree with. You need to be careful to make 
aspects of a language as consistent as possible and thus allowing no receiver 
of a function definition (somewhat different than no name) might result in 
anomalies in other parts of the language. Errors that could be caught would be 
accepted, even if teh resulting code was nonsense.
Some languages use a real placeholder such as "_" (single underscore) to 
represent an I DON'T CARE scenario meaning I do not want to provide a name and 
Python allows code like:
(a, _, c, _, e) = (1, 2, 3, 4, 5)
print(a, _, c, _, e)1 4 3 4 5
The above code snippet suggests that _ is allowed to be used multiple times and 
retains whatever is done last. But an experiment shows there is nothing special 
about underscore as a name in that replacing it with an x above gets the same 
results. It may not be implemented at the interpreter level as "throw away" or 
skip over, but I can imagine languages implementing just that. In the above, 
certainly, the reading in of the number 2 may have happened and the variable 
reused leaves it without one of the pointers to it.
But consider what happens if the matching above was to more complex items like 
function calls that may have all kinds of effects and side effects.  I may want 
to throw away the results returned, but may want the side effect. 
So in a sense the question being asked might have a partial suggestion, in 
Python, to simply provide a clue when creating the function to have the name _, 
albeit I still keep wondering WHY you want that.
Consider other things like a language in which just typing the name of a 
variable has no meaning. I mean it does not autoprint. If a line by itself says:
myvar
It typically starts a search for whatever method the language uses to find that 
name in their data structures to determine if it exists in the current context. 
If not found, you expect an error message. But even if found, what does one do 
with it? In some languages, there seems to be no point in even accessing it and 
it seems to be anything from harmless code to be ignored or an error because it 
is not a valid construct and the user may have meant to say myvar = 6 or 
something. But in a language like Python, it may have some impact just from 
being invoked such as incrementing a counter within an object. If you want to 
have such functionality in the language, you now have to allow it. And one way 
to support this is to make believe you typed print(myvar) which on my machine 
fails when myvar is not defined. When it is, it prints what it can. This works 
even when I ask for seemingly useless operations like "()" which creates and 
then (after the print) ignores an empty tuple. 
Anonymous functions arguably have an important place in many languages and I 
keep seeing languages adding some version of them or new syntax for them, as R 
recently did. But they are best used when made deliberately and the example we 
are given seems highly likely to be a mistake where the user forgot to put in a 
name where it was expected. At the very least it may merit a WARNING. Or, if 
allowed, I wonder if the interpreter should perhaps do something amusing like 
give it some long nonsense name and notify the user that in case they actually 
wanted a real function, it is now mxyzptlk666 ...


-Original Message-
From: Eryk Sun 
To: python-list@python.org
Sent: Mon, Feb 21, 2022 2:35 am
Subject: Re: Why does not Python accept functions with no names?

On 2/20/22, Greg Ewing  wrote:
>
> BTW, this is not what is usually meant by the term "anonymous
> function". An anonymous function is one that is not bound
> to *any* name. The thing you're proposing wouldn't be
> anonymous -- it would have a name, that name being the empty
> string.

Sorry. I read Avi's reply first, scanned the original post and didn't
consider the literal question. I doubt that Python would have become a
popular language if it allowed the empty string as an identifier. That
would let common syntax errors slip past the compiler with useless
results, e.g `= 1`, `c. = 1`. It's no less absurd in a `def`
statement.
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Eryk Sun
On 2/20/22, Greg Ewing  wrote:
>
> BTW, this is not what is usually meant by the term "anonymous
> function". An anonymous function is one that is not bound
> to *any* name. The thing you're proposing wouldn't be
> anonymous -- it would have a name, that name being the empty
> string.

Sorry. I read Avi's reply first, scanned the original post and didn't
consider the literal question. I doubt that Python would have become a
popular language if it allowed the empty string as an identifier. That
would let common syntax errors slip past the compiler with useless
results, e.g `= 1`, `c. = 1`. It's no less absurd in a `def`
statement.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Chris Angelico
On Mon, 21 Feb 2022 at 16:48, Avi Gross via Python-list
 wrote:
>
> Amusingly, Greg, if you had a system where the un-named anonymous function 
> was to be named the unique value of the empty string, then a second such 
> anonymous function definition would over-write it, as with any named 
> function. The kind of anonymous function we are more used to might be 
> something you can create as say elements of a list so you have a list of 
> functions you can access as in f[0] but in a sense that has a name as it can 
> be accessed as a component of the data structure called f.  I am not sure if 
> python would trivially let you create that. But the point is if you want to 
> be able to make many such pseudo-anonymous functions, in the end, there can 
> only be one.
>

Functions in Python have two different concepts of "name", which
aren't always the same. One is the way that you refer to it; the other
is its __name__ attribute (and related attributes like __qualname__).
An anonymous function doesn't necessarily have to lack a name, per se;
it simply doesn't have a very interesting name:

>>> (lambda: 1).__name__
''
>>> (lambda: 2).__name__
''
>>> (lambda: 3).__name__
''

If you want to refer to them as f[0] etc, they can have arbitrary
names, which may or may not themselves be meaningful. I frequently
build a dictionary or list of functions by decorating standard 'def'
statements:

funcs = []

@funcs.append
def spam(): ...
@funcs.append
def ham(): ...

They still have __name__ attributes, but it's absolutely fine to
double them up, since they're going to be looked up via the list.
(Though there's still value in having at least *some* name on them,
since it shows up in backtraces.)

There's very little reason to have a function that actually doesn't
have a name. It's usually fine to give it an arbitrary and meaningless
name.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Avi Gross via Python-list
Amusingly, Greg, if you had a system where the un-named anonymous function was 
to be named the unique value of the empty string, then a second such anonymous 
function definition would over-write it, as with any named function. The kind 
of anonymous function we are more used to might be something you can create as 
say elements of a list so you have a list of functions you can access as in 
f[0] but in a sense that has a name as it can be accessed as a component of the 
data structure called f.  I am not sure if python would trivially let you 
create that. But the point is if you want to be able to make many such 
pseudo-anonymous functions, in the end, there can only be one.


-Original Message-
From: Greg Ewing 
To: python-list@python.org
Sent: Sun, Feb 20, 2022 5:17 pm
Subject: Re: Why does not Python accept functions with no names?

On 21/02/22 6:27 am, Abdur-Rahmaan Janhangeer wrote:
> Well Python deliberately throws an exception if we do not
> pass in a function name. Just wanted to know why it should. As the
> above case is a 100% pure anonymous function.

The syntax for a function definition is defined in the grammar
as requiring an identifier. An identifier in turn is defined
as consisting of at least one character. So the grammar would
need to be changed to make the name optional. Also, the
compiler would need a special case to treat a missing name
there as though it were an empty string.

So again, why *should* it be allowed, given that the parser and
compiler would have to go out of their way to treat it as a
special case, only to create a function that there is no
easy way to call?

BTW, this is not what is usually meant by the term "anonymous
function". An anonymous function is one that is not bound
to *any* name. The thing you're proposing wouldn't be
anonymous -- it would have a name, that name being the empty
string.

-- 
Greg
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Grant Edwards
On 2022-02-20, Christian Gollwitzer  wrote:

>> For the same reason an empty sequence of characters cannot
>> be a variable name. Do you know any language (or formal
>> theory) that allows that?
>
> Tcl allows that:

Interesting to know, but the fact that Tcl does something differnt is
more of an argument against it.

My one expereince trying to write a non-trivial application was a
study in frustration.  After spending days trying to get Tcl to do
something useful, I finally gave up and wrote the program in Scheme in
a few hours.

--
Grant
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Chris Angelico
On Mon, 21 Feb 2022 at 14:36, Python  wrote:
>
> Barry wrote:
> >
> >
> >> On 20 Feb 2022, at 15:35, Abdur-Rahmaan Janhangeer  
> >> wrote:
> >>
> >> Greetings list.
> >>
> >> Out of curiosity, why doesn't Python accept
> >> def ():
> >> return '---'
> >>
> >> ()
> >>
> >> Where the function name is ''?
> >
> > Because there is no variable that is holding a ref to the code.
> > So it’s pointless.
>
> Fun fact, it can be done, but it's (afaik) then (almost) unusable...
>
>  >>> a
> Traceback (most recent call last):
>File "", line 1, in 
> NameError: name 'a' is not defined
>  >>> locals()['a'] = 42
>  >>> a
> 42
>  >>> locals()[''] = 42
>  >>> locals()['']
> 42
>  >>> locals()[''] = (lambda x: x*42)
>  >>> locals()[''](1)
> 42
>

Minor nitpick: Please use globals() rather than locals() for this sort
of demo. At module level (including the REPL), they are the same, but
inside a function there's no guarantee that locals() can be mutated in
this way.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Greg Ewing

On 21/02/22 6:27 am, Abdur-Rahmaan Janhangeer wrote:

Well Python deliberately throws an exception if we do not
pass in a function name. Just wanted to know why it should. As the
above case is a 100% pure anonymous function.


The syntax for a function definition is defined in the grammar
as requiring an identifier. An identifier in turn is defined
as consisting of at least one character. So the grammar would
need to be changed to make the name optional. Also, the
compiler would need a special case to treat a missing name
there as though it were an empty string.

So again, why *should* it be allowed, given that the parser and
compiler would have to go out of their way to treat it as a
special case, only to create a function that there is no
easy way to call?

BTW, this is not what is usually meant by the term "anonymous
function". An anonymous function is one that is not bound
to *any* name. The thing you're proposing wouldn't be
anonymous -- it would have a name, that name being the empty
string.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Python

Barry wrote:




On 20 Feb 2022, at 15:35, Abdur-Rahmaan Janhangeer  wrote:

Greetings list.

Out of curiosity, why doesn't Python accept
def ():
return '---'

()

Where the function name is ''?


Because there is no variable that is holding a ref to the code.
So it’s pointless.


Fun fact, it can be done, but it's (afaik) then (almost) unusable...

>>> a
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'a' is not defined
>>> locals()['a'] = 42
>>> a
42
>>> locals()[''] = 42
>>> locals()['']
42
>>> locals()[''] = (lambda x: x*42)
>>> locals()[''](1)
42

--
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Python

Abdur-Rahmaan Janhangeer wrote:

Greetings list.

Out of curiosity, why doesn't Python accept
def ():
 return '---'

()

Where the function name is ''?


For the same reason an empty sequence of characters cannot
be a variable name. Do you know any language (or formal
theory) that allows that?


--
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Christian Gollwitzer

Am 20.02.22 um 16:48 schrieb Python:

Abdur-Rahmaan Janhangeer wrote:

Greetings list.

Out of curiosity, why doesn't Python accept
def ():
 return '---'

()

Where the function name is ''?


For the same reason an empty sequence of characters cannot
be a variable name. Do you know any language (or formal
theory) that allows that?


Tcl allows that:

Main console display active (Tcl8.6.9 / Tk8.6.9)
(CDEF) 49 % set "" Hallo
Hallo
(CDEF) 50 % puts ${}
Hallo
(CDEF) 51 % proc "" {} { puts "I'm empty" }
(CDEF) 52 % ""
I'm empty
(CDEF) 53 %

Any string can be a variable or command name, only :: is special as a 
namespace separator.


This only works because of the sparse syntax; to retrieve a variable's 
content, $ is used. For "strange" names quoting is required, therefore I 
had to use "" in the example.


It's a different matter how useful this actually is. One of the object 
systems in Tcl uses the empty variable to represent "self" as an array, 
so that you can write $(prop) for self.prop as it is in Python.


Christian


--
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Chris Angelico
On Mon, 21 Feb 2022 at 09:38, Eryk Sun  wrote:
>
> On 2/20/22, Abdur-Rahmaan Janhangeer  wrote:
> >
> > Out of curiosity, why doesn't Python accept
> > def ():
> > return '---'
>
> The `def` keyword is compiled as an assignment statement, not an
> expression that evaluates anonymously on the stack. Using `def` as an
> expression would require new syntax. For example, maybe something like
> the following:
>
> funcs[i] = def (x, *, y=0) := (
> if x < y:
> return spam(x)
> return eggs(x)
> )
>

There's always decorators.

>>> @functools.partial(operator.setitem, funcs, i)
... def _(x, *, y=0):
... if x < y:
... return spam(x)
... return eggs(x)
...
>>> funcs[i]


(Has the side effect of setting _ to None.)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Eryk Sun
On 2/20/22, Abdur-Rahmaan Janhangeer  wrote:
>
> Out of curiosity, why doesn't Python accept
> def ():
> return '---'

The `def` keyword is compiled as an assignment statement, not an
expression that evaluates anonymously on the stack. Using `def` as an
expression would require new syntax. For example, maybe something like
the following:

funcs[i] = def (x, *, y=0) := (
if x < y:
return spam(x)
return eggs(x)
)

Since parsing Python depends on white space, if a `def (...) :=`
expression is multiline, the first statement on a new line would set
the indentation level, up to but not including the closing
parenthesis. (Style guides would likely restrict what's considered
good form.) The anonymous function could be called immediately. For
example:

results[i] = def (x, *, y=0) := (
if x < y:
return spam(x)
return eggs(x)
)(z)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Avi Gross via Python-list
Not really. Anonymous functions are anonymous in name only, so to speak.
When you call a function and pass it an argument that is an anonymous function 
definition, it is bound to a variable within the function and used mostly in a 
non-anonymous way. You did not bother giving it a name but it is referenced and 
retains an existence until it goes out of scope. Not having a name within your 
own scope means you do not have to do anything like deleting it further in your 
code. But most code that uses anonymous functions could be rewritten to use a 
named function. 
However, a compiler or interpreter may well be optimized to decide to ignore or 
even rewrite things. Consider what it might do to something like "if (5 < 4) 
..." where all the code in the ellipsis can never be reached and hence it and 
the entire statement can be silently removed or ignored OR perhaps reported as 
a logical error. The code is absolutely valid in terms of syntax, but not valid 
as having any purpose. But what if all the above is the body of another 
statement like an "else" which would become a problem if you removed this code? 
Obviously you might have to walk up the tree and trim more.
Declaring an anonymous function in a context where it attaches to no names 
anywhere can be allowed to proceed, if that is important to anyone, but garbage 
collection will rapidly reclaim the space, I would think. So why bother 
allocating the space? And note if the allocation method is odd and leaves it 
entirely out of the tables that keep track, you then would have a memory leak.


-Original Message-
From: Abdur-Rahmaan Janhangeer 
To: Chris Angelico 
Cc: Python 
Sent: Sun, Feb 20, 2022 12:27 pm
Subject: Re: Why does not Python accept functions with no names?

Thanks for the answers.

@Chris Well Python deliberately throws an exception if we do not
pass in a function name. Just wanted to know why it should. As the
above case is a 100% pure anonymous function.

Kind Regards,

Abdur-Rahmaan Janhangeer
about <https://compileralchemy.github.io/> | blog
<https://www.pythonkitchen.com>
github <https://github.com/Abdur-RahmaanJ>
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Dieter Maurer
Abdur-Rahmaan Janhangeer wrote at 2022-2-20 19:32 +0400:
>Out of curiosity, why doesn't Python accept
>def ():
>return '---'
>
>()
>
>Where the function name is ''?

Python knows about (somewhat restricted) anonymous functions:
it calls them `lambda` expressions (the body of those functions
can only be an expression).

Your example above can be expressed as
`(lambda: '---')()`.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Abdur-Rahmaan Janhangeer
Yes I know about lambdas but was just an introspection about
the reasoning behind ^^

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Abdur-Rahmaan Janhangeer
Thanks for the answers.

@Chris Well Python deliberately throws an exception if we do not
pass in a function name. Just wanted to know why it should. As the
above case is a 100% pure anonymous function.

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Chris Angelico
On Mon, 21 Feb 2022 at 02:33, Abdur-Rahmaan Janhangeer
 wrote:
>
> Greetings list.
>
> Out of curiosity, why doesn't Python

This is often the wrong question. The question is more: Why should Python?

Python doesn't do things just because there's no reason not to.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Dan Stromberg
On Sun, Feb 20, 2022 at 7:33 AM Abdur-Rahmaan Janhangeer <
arj.pyt...@gmail.com> wrote:

> Greetings list.
>
> Out of curiosity, why doesn't Python accept
> def ():
> return '---'
>
> ()
>
> Where the function name is ''?
>

() is already an empty tuple.  It would break code to change this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why does not Python accept functions with no names?

2022-02-20 Thread Barry


> On 20 Feb 2022, at 15:35, Abdur-Rahmaan Janhangeer  
> wrote:
> 
> Greetings list.
> 
> Out of curiosity, why doesn't Python accept
> def ():
>return '---'
> 
> ()
> 
> Where the function name is ''?

Because there is no variable that is holding a ref to the code.
So it’s pointless.


Barry

> 
> Kind Regards,
> 
> Abdur-Rahmaan Janhangeer
> about  | blog
> 
> github 
> Mauritius
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list


Why does not Python accept functions with no names?

2022-02-20 Thread Abdur-Rahmaan Janhangeer
Greetings list.

Out of curiosity, why doesn't Python accept
def ():
return '---'

()

Where the function name is ''?

Kind Regards,

Abdur-Rahmaan Janhangeer
about  | blog

github 
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list