# Code blocks and a possible syntax

**URL:** <https://discuss.python.org/t/code-blocks-and-a-possible-syntax/14673>\
**Category:** Ideas\
**Created:** [March 28, 2022, 11:24pm UTC](https://discuss.python.org/t/code-blocks-and-a-possible-syntax/14673 "2022-03-28T23:24:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_builtin.self](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/_builtin.self/32/31874_2.png) [@\_builtin.self](https://discuss.python.org/u/_builtin.self)\
**Post date:** [March 28, 2022, 11:24pm UTC](https://discuss.python.org/t/code-blocks-and-a-possible-syntax/14673/1 "2022-03-28T23:24:36Z")

</div>

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 :

```auto
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

```auto
class my_transtormer(ast.NodeTransformer):
    def vist(self, node) :
        return node

@my_transformer().vist
code_block :
    print("hello world!") 

```

With this usage the code in the code block is converted to an ast node before being passed to the decorator. The code is then executed.

The last use is to create a piece code to store for later use.

```auto
code_block as my_code:
    print (" hello world")

#nothing is printed immediately

exec (my_code) # results in "hellow world" being printed

```

This allows code to be created and stored without needing to store it as a string well as allowing the user of decorators.

---

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [March 29, 2022, 3:43am UTC](https://discuss.python.org/t/code-blocks-and-a-possible-syntax/14673/2 "2022-03-29T03:43:18Z")

</div>

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’m also going to preempt the discussion of inserting code directly into the AST by readying the inline-function discussions: [Inline Python functions and methods - Ideas - Discussions on Python.org](https://discuss.python.org/t/inline-python-functions-and-methods/12412) and [Adding inline in Python - Ideas - Discussions on Python.org](https://discuss.python.org/t/adding-inline-in-python/13532)

---

<div class="post-metadata">

**Author:** ![\_builtin.self](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/_builtin.self/32/31874_2.png) [@\_builtin.self](https://discuss.python.org/u/_builtin.self)\
**Post date:** [March 29, 2022, 12:08pm UTC](https://discuss.python.org/t/code-blocks-and-a-possible-syntax/14673/3 "2022-03-29T12:08:08Z")

</div>

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:

```auto
def temp_func() :
    print("very important code") 

tree=ast.parse(inspect. getsource(temp_func), __name__ ) 
my_transtormer().visit(tree)
tree[1].name="_"
code=compile(ast.Module([tree[1],
           ast.Return(ast.name("_",ast.Load()) )])
eval(code)() 

```

As opposed to my proposed syntax:

```auto
@my_transtormer().visit
code_block:
    print("very important code ") 

```

---

<div class="post-metadata">

**Author:** ![TobiasHT](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tobiasht/32/23304_2.png) [@TobiasHT](https://discuss.python.org/u/TobiasHT)\
**Post date:** [March 29, 2022, 3:39pm UTC](https://discuss.python.org/t/code-blocks-and-a-possible-syntax/14673/4 "2022-03-29T15:39:52Z")

</div>

Does this correspond to Labels and goto in C?

---

<div class="post-metadata">

**Author:** ![\_builtin.self](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/_builtin.self/32/31874_2.png) [@\_builtin.self](https://discuss.python.org/u/_builtin.self)\
**Post date:** [March 29, 2022, 4:45pm UTC](https://discuss.python.org/t/code-blocks-and-a-possible-syntax/14673/5 "2022-03-29T16:45:42Z")

</div>

No. It is a way to make using an ast modifier easier.

---

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [March 29, 2022, 11:39pm UTC](https://discuss.python.org/t/code-blocks-and-a-possible-syntax/14673/6 "2022-03-29T23:39:38Z")

</div>

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?

* * *

1. AST: [abstract syntax tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree), for those who don’t know
