Idea
Add syntax for structural comments to Python.
This does not need to cover all structures initially, and can be contributed to iteratively.
What are structural comments?
A structural comment will mark a structure as a comment to the interpreter/compiler.
For example:
def vec_add(x, y):
z = []
for i in range(0, min(len(x), len(y)):
z.append(x[i] + y[i])
return z
In this function, we have some example structures:
- The function scope itself;
def ...
- Parameters for the function
x,
andy,
- Assignment of z;
z = []
- A for loop
for ...
- A function invocation;
len(x)
- A method invocation; `z.append(…)
…
Supposing that we wanted to comment out this code, today it would be done line-wise.
# def vec_add(x, y):
# z = []
# for i in range(0, min(len(x), len(y)):
# z.append(x[i] + y[i])
# return z
With structural comments, we could comment out structures (using example syntax of #_
):
Comments out the entire function…
#_def vec_add(x, y):
z = []
for i in range(0, min(len(x), len(y)):
z.append(x[i] + y[i])
return z
Comments out the entire parameter…
def vec_add(#_x, y):
z = []
for i in range(0, min(len(x), len(y)):
z.append(x[i] + y[i])
return z
Comments out the entire for loop…
def vec_add(x, y):
z = []
#_for i in range(0, min(len(x), len(y)):
z.append(x[i] + y[i])
return z
There are cases where this is equivalent to a line comment. There are cases where it is not.
How is this done now?
Usually, support from an editor providing a hotkey or an interaction menu, where each line is commented.
Commenting out a parameter of a function is not supported, though, unless that parameter is on its own line.
Indistinguishable Cases
I’m not aware of any cases where the compiler cannot distinguish the structure being commented out, hopefully they can be uncovered during discussion
Proposed Syntax
No syntax is currently being proposed for this change, though #_
is an example provided for illustrative purposes.
Benefit to Programmers
Many programmers frequently use comments during prototyping, especially during debugging or when code is particularly complex.
EDIT: was writing this on my phone and prematurely submitted.