# Is there @namedtuple for Python?

**URL:** <https://discourse.julialang.org/t/is-there-namedtuple-for-python/100681>\
**Category:** Offtopic\
**Tags:** python\
**Created:** [June 22, 2023, 12:58am UTC](https://discourse.julialang.org/t/is-there-namedtuple-for-python/100681 "2023-06-22T00:58:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xgdgsc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xgdgsc/32/608_2.png) [@xgdgsc](https://discourse.julialang.org/u/xgdgsc)\
**Post date:** [June 22, 2023, 12:58am UTC](https://discourse.julialang.org/t/is-there-namedtuple-for-python/100681/1 "2023-06-22T00:58:20Z")

</div>

[NamedTupleTools.jl](https://github.com/JeffreySarnoff/NamedTupleTools.jl) ’ s `@namedtuple` is really nice for wrapping function returns quickly. Is there a similar thing in Python that only need to type once of the value? I didn’ t find one with search.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [June 22, 2023, 1:57am UTC](https://discourse.julialang.org/t/is-there-namedtuple-for-python/100681/2 "2023-06-22T01:57:56Z")

</div>

Python does not have syntax-level macros, so what you are asking for is rather not possible without very significant abuse of the language. On the other hand, python is famous for very significant abuse of the language, so you might be able to build some very hacky solution. Good places to start looking are the AST module, import hooks, eval, and company.

If you can permit yourself to encapsulate your expression into a string, then things become much simpler, but you then need to make your own parser for the language and lose IDE support.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 22, 2023, 3:43am UTC](https://discourse.julialang.org/t/is-there-namedtuple-for-python/100681/3 "2023-06-22T03:43:30Z")

</div>

By default, you can construct a named tuple in julia by using the variable names as keys:

```julia
julia> a = 1; b = 2
2

julia> (; a, b)
(a = 1, b = 2)

julia> (; a, b, c = 3)
(a = 1, b = 2, c = 3)

```

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [June 22, 2023, 1:30pm UTC](https://discourse.julialang.org/t/is-there-namedtuple-for-python/100681/4 "2023-06-22T13:30:18Z")

</div>

Maybe you could use [PythonCall](https://juliahub.com/ui/Packages/PythonCall/WdXsa/0.9.13) and have Julia do the heavy lifting?

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [June 22, 2023, 3:52pm UTC](https://discourse.julialang.org/t/is-there-namedtuple-for-python/100681/5 "2023-06-22T15:52:57Z")

</div>

No you can’t do this.

The Pythonic way is to create a new named tuple type at the top level

```julia
from collections import namedtuple
Foo = namedtuple("Foo", "x y z")

```

and construct it when needed

```julia
Foo(1, 2, 3)

```

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [June 22, 2023, 4:13pm UTC](https://discourse.julialang.org/t/is-there-namedtuple-for-python/100681/6 "2023-06-22T16:13:23Z")

</div>

Well it’s possible, it just looks differently 😉 You can wrap the function’s return values by directly passing it to the `namedtuple` constructor (as shown by @cjdoris) with `*` (similar to Julia’s splat `...`) .

```python
>>> from collections import namedtuple
... Foo = namedtuple("Foo", "x y z")

>>> def foo():
... return (1, 2, 3)
...

>>> Foo(*foo())
Foo(x=1, y=2, z=3)

```
