Straight Ahead, on average,

I am not sure what your beef is as apparently it is always something else than 
some others assumed.

If your point is that you want consistency, sure that would be nice. But maybe 
part of the inconsistency I am not sure you mean is an artifact of the language.

There are programming languages where a keyword like "def" is used to create a 
function and similarly some where variables are declared some special way like 
"let" or "var" and "const" or "class" to provide the right hints. Some 
languages skip that and just let you pop up a variable as in "x=5" both 
declares and instantiates a variable. Strictly speaking, in a language like 
python, everything is an object and some of the way of creating them remains 
bound to other ideas like declaring a class uses key words as does declaring a 

But consider a language like R which declares a function similar to anything 

X <- 5
Times2 <- function(x) { x*2 }

The keyword has been moved as compared to the python:

Def Times2(x):

It is not a question of being better or worse, but simply speaking, there is no 
special keyword to start the process before the name, but rather a keyword 
later that is really similar to a lambda method. Secondarily, not relying on 
indentation, may make it easier in some cases to insert the function 
declaration anywhere a statement might fit as there is no special keyword.

Other languages have their own chosen ways and many will fail various tests you 
can come up with.

Could python have chosen some other way that would have fit some grammar needs? 
Probably. But it did not.

If you want to play a common grammar game, you can define a higher-level 
construct I will simply call A  that is decomposed into two or more subcontexts 
one of which is B and then populate the tree on each side with more and more 
specific stuff. Then when talking about what is allowed in context alpha, where 
all expressions are allowed, say it supports A or anything in the tree below 
it. In another context, say it only supports B and anything below that.

Think of it a bit like subclassing.

Perhaps you can then build a complex description that can be instantiated by 
code to define all valid programs from invalid ones. But it would remain a pain 
to implement all the tools you want to help you, including in an editor or 
programming environment, where figuring out the context may become very 

Still, making things very loose and general so that your design looks simple 
has many negative tradeoffs too, including allowing rather nonsensical things.

People who create programming languages have various goals in mind that guide 
what they choose. Python was not made to be a mathematically perfect object 
that guided being able to have programs proven to work and so on. It is 
acknowledged various aspects do not please some people or others and I am not 
defending it.

I am wondering if what is being discussed is in any way a serious issue.

The original question in this thread really was a minor one and how it became 
whatever this is, well, I give up! LOL!

-----Original Message-----
From: Left Right <> 
Sent: Sunday, January 14, 2024 4:15 PM
Cc: Chris Angelico <>;
Subject: Re: Extract lines from file, add to new files

> You said function. I made a function. You said "head of a for loop
> clause". I put it there. Problem was underspecified.

I also wrote a lot of letters, if you combine them very liberally,
without any regard to the order in which they were written or the
context in which they were used, you may come up with very surprising

> But if you're trying to tell me that a def statement should be a valid
> assignment target,

Why not just read what I wrote and work from there?  No, I didn't
write anything even remotely similar to this...  I don't want function
definition to be an assignment target.  I was giving an example of how
Python grammar works, how the rules govern what can or cannot be used
in a particular place...

In other words, if you aren't sure you understand the question, why
are you trying to reply to it? Is your goal to learn the meaning of
the question by giving arbitrary replies and hoping that the author of
the question restates it so that you understand it?  If so, I believe,
the better strategy would be to simply ask to restate the question.
Will save you the round-trip.

> You provided a way to create an anonymous function and that was not enough.
> I wonder if you could throw in the new := walrus operator to similarly make
> a named lambda function in a similar way.

The person you are replying to didn't understand the question and has
written something irrelevant.  It's not about being "enough".  I
honestly don't know why they are spending so much energy replying to
my messages :|

> Python grew and there was regular pressure to add keywords which might break
> existing programs. So, yes, sometimes, a keyword was re-used in a different
> context.

Why are keywords relevant to this?

> How often do you really think anyone out there NEEDS to define a function in
> the context mentioned?

This isn't about programmers writing programs that aren't about the
language.  It's about programmers who write language-related tools,
like linters, formatters etc.  I.e. the programmers who need to
consider any possible grammar product. And the reason I mentioned
function definition is, this, again: function definition is a
statement.  Python grammar rules prevent function definition from
appearing in left-hand side of the head of the for loop.  However, a
variable declaration, which is also a statement, is allowed there.
Programmers like grammar rules to be consistent, and it's surprising
if a particular larger context allows both statements and expressions.
I also explained why and how language authors would make a decision to
break this consistency: it saves some keystrokes for the programmers.
I.e. allows for shorter programs, while doesn't add any new abilities
to the language.


Reply via email to