# Returning value from exec()

**URL:** <https://discuss.python.org/t/returning-value-from-exec/107503>\
**Category:** Python Help\
**Created:** [May 23, 2026, 5:52pm UTC](https://discuss.python.org/t/returning-value-from-exec/107503 "2026-05-23T17:52:48Z")\
**Posts on this page:** 1\
**Showing post:** 23

<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:** [May 27, 2026, 12:52pm UTC](https://discuss.python.org/t/returning-value-from-exec/107503/23 "2026-05-27T12:52:50Z")

</div>

> [@NickC](#):
>
> My example does have what you describe as ‘a bare return outside a function’ which you state ‘isn’t valid python syntax’; question is why not? As Python has a return statement why does it then preclude it’s use from any piece of Python code as in my example? It would surely be quite simple to allow any piece of code to return a value to say an exec() function.
> 
> Deliberately restricting ‘return’ to use only from a function seems preversely pedantic to me. It may not be ‘Pythonic’ but what happened to allowing everyone to behave like adults and make their own decisions upon whether it should be used ot not.

Before complaining like this, I suggest you design your own programming language. It would be an excellent exercise in language design, and you’d learn all sorts of things about what’s good, what’s bad, and (most of all) what’s a compromise between two ideals.

---

_[View the full topic](https://discuss.python.org/t/returning-value-from-exec/107503)._
