# Function annotation behavior when a function is inserted rather than a callable

**URL:** <https://discuss.python.org/t/function-annotation-behavior-when-a-function-is-inserted-rather-than-a-callable/19147>\
**Category:** Ideas\
**Created:** [September 15, 2022, 10:18pm UTC](https://discuss.python.org/t/function-annotation-behavior-when-a-function-is-inserted-rather-than-a-callable/19147 "2022-09-15T22:18:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![carter-canedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/carter-canedy/32/8948_2.png) [@carter-canedy](https://discuss.python.org/u/carter-canedy)\
**Post date:** [September 15, 2022, 10:18pm UTC](https://discuss.python.org/t/function-annotation-behavior-when-a-function-is-inserted-rather-than-a-callable/19147/1 "2022-09-15T22:18:29Z")

</div>

Is there a reason why we’re able to have a function execute when defining a function and using a function annotation?

This snippet

```python
def Foo() -> None:
    print("Bar")

def FooBar() -> Foo():
    return None

```

will print to the terminal:

```auto
$ Bar

```

The behavior is the same for object callables, as well:

```auto
class Foo:
    def __init__ ():
        print("Bar")

def FooBar() -> Foo():
    return None

```

will also print to the terminal:

```auto
$ Bar

```

Is there a use-case for inserting a function following a function annotation to call an extraneous function? I feel like it’s more of a bug, but I might be wrong.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [September 15, 2022, 10:22pm UTC](https://discuss.python.org/t/function-annotation-behavior-when-a-function-is-inserted-rather-than-a-callable/19147/2 "2022-09-15T22:22:16Z")

</div>

In Python, pretty much everything is simply defined as “an expression”. It’s extremely useful to be able to do things like `List[int]` which is subscripting one object with another; and since it makes sense to have that kind of flexibility, it’s not worth locking things down and putting arbitrary restrictions on the expression involved.

(There HAVE been times when restrictions have been applied, such as function decorators. That particular one got lifted recently, but others do exist. They’re rare though.)

---

<div class="post-metadata">

**Author:** ![notatallshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/notatallshaw/32/10861_2.png) [@notatallshaw](https://discuss.python.org/u/notatallshaw)\
**Post date:** [September 16, 2022, 1:38am UTC](https://discuss.python.org/t/function-annotation-behavior-when-a-function-is-inserted-rather-than-a-callable/19147/3 "2022-09-16T01:38:02Z")

</div>

Annotations in their current form act like an additional expression that can be run after any line of code, e.g. The following prints Hello and then Goodbye.

```python
foo: print('Goodbye') = print('Hello')

```

Currently there’s nothing special about this expression, it immediately evaluates just like any other expression in Python

For a long time it was intended that this immediate evaluation of annotations would be removed as part of "[PEP 563 – Postponed Evaluation of Annotations | peps.python.org](https://peps.python.org/pep-0563/) " but more recently it seems like a similar idea but a different approach to not immediately evaluate annotations might be what replaces the current behavior: “[PEP 649 – Deferred Evaluation Of Annotations Using Descriptors | peps.python.org](https://peps.python.org/pep-0649/)”

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [September 16, 2022, 3:13am UTC](https://discuss.python.org/t/function-annotation-behavior-when-a-function-is-inserted-rather-than-a-callable/19147/4 "2022-09-16T03:13:21Z")

</div>

> [@quantzur](#):
>
> Annotations in their current form act like an additional expression that can be run after any line of code, e.g. The following prints Hello and then Goodbye.
> 
> ```auto
> foo: print('Goodbye') = print('Hello')
> 
> ```

Be careful here: what you’re doing is annotating the variable `foo` and also assigning it a value. It’s equivalent to this statement:

```
foo = print('Hello')

```

with the additional information that:

```
__annotations__ ['foo'] = print('Goodbye')

```

This will work on assignments, but not on arbitrary statements. What you’re really doing here is annotating the _variable_, and also initializing it, so you don’t want to have multiple annotations attached to the same variable multiple times.

Annotations are only connected to certain types of thing, mainly variables/functions in different forms, and are not actually associated with the statements that created them.

(I am a little surprised that the assignment expression is evaluated prior to the annotation. It’s unusual, in Python, for evaluation to happen in reverse order like that.)

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [September 16, 2022, 4:23am UTC](https://discuss.python.org/t/function-annotation-behavior-when-a-function-is-inserted-rather-than-a-callable/19147/5 "2022-09-16T04:23:23Z")

</div>

> [@Rosuav](#):
>
> (I am a little surprised that the assignment expression is evaluated prior to the annotation. It’s unusual, in Python, for evaluation to happen in reverse order like that.)

Oh, interesting. Perhaps a side effect of evaluating the RHS before the LHS for the ‘=‘ operator. See [7. Simple statements — Python 3.12.1 documentation](https://docs.python.org/3/reference/simple_stmts.html), especially this:

> Although the definition of assignment implies that overlaps between the left-hand side and the right-hand side are ‘simultaneous’ (for example `a, b = b, a` swaps two variables), overlaps within the collection of assigned-to variables occur left-to-right, sometimes resulting in confusion. For instance, the following program prints `[0, 2]`:
> 
> ```auto
> x = [0, 1]
> i = 0
> i, x[i] = 1, 2 # i is updated, then x[i] is updated
> print(x)
> 
> ```

---

<div class="post-metadata">

**Author:** ![notatallshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/notatallshaw/32/10861_2.png) [@notatallshaw](https://discuss.python.org/u/notatallshaw)\
**Post date:** [September 16, 2022, 4:37am UTC](https://discuss.python.org/t/function-annotation-behavior-when-a-function-is-inserted-rather-than-a-callable/19147/6 "2022-09-16T04:37:08Z")

</div>

I guess this left to right rule of the LHS hold visually for annotations also, if the annotation throws an exception the assignment still happens:

```python
>>> i: 1/0 = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> i
1
>>> __annotations__
{}

```

In my reasoning of annotations I’ve always considered them to be placed “between the current line and the next line”, but maybe that was an oversimplification.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [September 16, 2022, 7:41am UTC](https://discuss.python.org/t/function-annotation-behavior-when-a-function-is-inserted-rather-than-a-callable/19147/7 "2022-09-16T07:41:00Z")

</div>

> [@guido](#):
>
> > [@Rosuav](#):
> >
> > (I am a little surprised that the assignment expression is evaluated prior to the annotation. It’s unusual, in Python, for evaluation to happen in reverse order like that.)
> 
> Oh, interesting. Perhaps a side effect of evaluating the RHS before the LHS for the ‘=‘ operator.

Could be. Part of my brain wants to think of annotations as comments (going all the way to the right of the executable part of the line), but if they’re written that way, they’re not evaluated _at all_, so that’s not really a precedent.
