# How namespace of Base.Iterators works

**URL:** <https://discourse.julialang.org/t/how-namespace-of-base-iterators-works/58854>\
**Category:** General Usage\
**Tags:** question, iterators, namespaces\
**Created:** [April 8, 2021, 4:51pm UTC](https://discourse.julialang.org/t/how-namespace-of-base-iterators-works/58854 "2021-04-08T16:51:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [April 8, 2021, 4:51pm UTC](https://discourse.julialang.org/t/how-namespace-of-base-iterators-works/58854/1 "2021-04-08T16:51:05Z")

</div>

I like to code using more functional-like style working with iterators, so `Base.Iterators` comes in handy, but I have problem understanding how its namespace is structured.

- It is part of the `Base`, yet it is not by default loaded in your namespace.
- If you load it you need full name `Base.Iterators` not just `Iterators`.
- Some commands are always available (`zip, enumerate`), others do not.
- Even you write `using Base.Iterators` some commands still need a full name (`Iterators.peel`), others do not.

I am just confused. Is there any rule to this? The documentation of `Iterators` seems not to be helping much. I mix this all the time, it is hard to remember what exactly I need to write in each case.

---

<div class="post-metadata">

**Author:** ![simonschoelly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonschoelly/32/21606_2.png) [@simonschoelly](https://discourse.julialang.org/u/simonschoelly)\
**Post date:** [April 8, 2021, 5:36pm UTC](https://discourse.julialang.org/t/how-namespace-of-base-iterators-works/58854/2 "2021-04-08T17:36:40Z")

</div>

`Iterators` is a submodule of `Base`. While `Iterators` exports some symbols, they are not reexported by `Base`, therefore they are not automatically in scope. You can bring them in scope by writing

```julia
using Base.Iterators

```

This will only bring these symbols into scope that are exported by `Iterators`. (To see which ones, write `names(Iterators)`). Non-exported symbols can then be accessed by writing `Iterators.some_name`.

Maybe looking at the code of `Iterators` will help you understand: [julia/iterators.jl at master · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/master/base/iterators.jl).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 8, 2021, 8:43pm UTC](https://discourse.julialang.org/t/how-namespace-of-base-iterators-works/58854/3 "2021-04-08T20:43:08Z")

</div>

I think the mechanism that causes these inconveniences is not the object of the question, but why some things are reexported, why some are just exported, and why some are not exported nor reexported. Why different decisions were taken for each function, the rhyme and reason for it, and if it is some convention that can be easily remembered.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [April 8, 2021, 10:13pm UTC](https://discourse.julialang.org/t/how-namespace-of-base-iterators-works/58854/4 "2021-04-08T22:13:32Z")

</div>

I agree that that is confusing.

Apparently some of the things that are not re-exported are that way because they have names that would conflict with things from base, for example `Base.Iterators.map` or `Base.Iterators.filter`  
See this discussion as an example  
[https://github.com/JuliaLang/julia/pull/34352](https://github.com/JuliaLang/julia/pull/34352)

And this one from when Base.Iterators was created

[https://github.com/JuliaLang/julia/pull/18839](https://github.com/JuliaLang/julia/pull/18839)

---

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [April 8, 2021, 11:40pm UTC](https://discourse.julialang.org/t/how-namespace-of-base-iterators-works/58854/5 "2021-04-08T23:40:35Z")

</div>

Yes, I was asking for some reason/rule or at least where it is written in the documentation. Looking into source code is not very convenient, but at least gives this info.

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [April 9, 2021, 12:04am UTC](https://discourse.julialang.org/t/how-namespace-of-base-iterators-works/58854/6 "2021-04-09T00:04:43Z")

</div>

> [@Tbl](#):
>
> If you load it you need full name `Base.Iterators` not just `Iterators` .

Note that you can use `using .Iterators`, because it is in fact in your namespace by default, you just don’t have its exports imported (aside from those that are reexported in Base). I think the selection of what is (re)exported and what not is just one of convenience and not set in stone. (I’d vote for an export of `peel` for example, and maybe reexported `flatten`, but knowing what is where comes fairly quickly. I’d just always use `Iterators.whatever` for anything not `zip` or `enumerate`.)

---

<div class="post-metadata">

**Author:** ![Tbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbl/32/5984_2.png) [@Tbl](https://discourse.julialang.org/u/Tbl)\
**Post date:** [April 9, 2021, 12:44pm UTC](https://discourse.julialang.org/t/how-namespace-of-base-iterators-works/58854/7 "2021-04-09T12:44:49Z")

</div>

I forgot about `.ModuleName`, it’s more elegant, thanks. I guess it is indeed best to always write `Iterators.f`

I also like `peel`, often you have a need to do something special during first iteration.
