How to recover \pi as a constant instead of a variable defined later in julia?

#1

I am learning Julia language recently. I defined a

\pi=1


How can I return to its original value without restarting Julia?

#2

\pi = Base.\pi

#3

This means you now have defined a variable that has the same value as Base.\pi while stored in a different place, right?

#4

I believe it assigns a pointer to \pi that points to Base.\pi

#5

This means you now have defined a variable that has the same value as Base.\pi while stored in a different place, right?

I would not worry about that. Mathematical constants are so called singleton types. Base already defines two alias Base.pi and Base.π and for all purposes they are the same. Main.π is actually already an imported binding. These constants do not take up “space”:

julia> x = Vector{Irrational{:π}}(typemax(Int))
9223372036854775807-element Array{Irrational{:π},1}:
π = 3.1415926535897...
π = 3.1415926535897...
π = 3.1415926535897...
...


#6

Some clarification that may be helpful here:

Most Julia functions are imported from a module called Base. Base is special because all Julia code has an implicit using Base. When you start the Julia binary, you are placed into a module called Main. Therefore, every time you assign a variable you are assigning variables in the namespace of Main (unless you create a module block or use eval to explicitly evaluate in another module).

So, when you do π = 1 you are setting the variable π in Main to no longer reference Base.π. Meanwhile, Base.π is untouched.

In Julia modules are a bit like a cross between a namespace and a class in an OO language (though you should not think of them as classes). They are basically there to provide the functionality of a namespace, but code is allowed to run inside them, sort of as if they each had their own runtime.

That’s a good way to think about it from the perspective of modules and namespaces, but it may not be the case in memory (in this case it isn’t). Basically it’s doing what @Juser said.

#7

Thank you very much. It’s very clear now.