Miscellaneous idea I had. Code blocks! A way to:
A create a new scope
B modify python code without needing to wrap it in a function first
C store code in a pythonic way
The simplest example of the concept is :
code_bock :
#statments go here
do_important_thing()
foo="bar"
print("foo is", foo) #prints "foo is bar"
print("foo is", foo) #fails because foo is not defined in this scope
The main other uses are modifying the code mid-execution
What’s the disadvantage of using a function? To a regular user, this will look like just a function without the def. You can always modify the behaviour of a function by writing if-statements in the function (granted, you can’t do this after the fact, but I would say that’s a benefit for code readability)
I meant the decorator syntax as a way to modify the ast of code then run it immediately. Not as an alternative to def. Currently if you want to modify the ast of some code the only way is:
Is there a good reason to have an AST[1] modifier? I can tell you from experience that having complex function decorators drastically increases the cognitive burden on future readers of the code. What’s an example where you would want to modify the AST and it wouldn’t make sense to achieve the same outcome more explicitly?