# Import same module as two independent identities

**URL:** <https://discourse.julialang.org/t/import-same-module-as-two-independent-identities/88132>\
**Category:** General Usage\
**Tags:** question, module\
**Created:** [October 2, 2022, 8:18pm UTC](https://discourse.julialang.org/t/import-same-module-as-two-independent-identities/88132 "2022-10-02T20:18:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DerongXu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derongxu/32/23102_2.png) [@DerongXu](https://discourse.julialang.org/u/DerongXu)\
**Post date:** [October 2, 2022, 8:18pm UTC](https://discourse.julialang.org/t/import-same-module-as-two-independent-identities/88132/1 "2022-10-02T20:18:32Z")

</div>

Suppose I have a module

```julia
module A

x=1

function set(y)
    global x
    x=y
end

end

```

Is there a way to import A twice, so that I have two different variables A.x as following

```julia
import A as A1
#A1.x=41
A1.set(41)
import A as A2
#A2.x=1024
A2.set(1024)
# A2.x and A1.x are two different variables

```

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [October 2, 2022, 9:47pm UTC](https://discourse.julialang.org/t/import-same-module-as-two-independent-identities/88132/2 "2022-10-02T21:47:12Z")

</div>

I don’t believe so, but why do you want this?

I’d recommend using structs:

```julia
mutable struct A{T}
    x::T
end
set!(a::A, y) = a.x = y
function Base.getproperty(a::A, s::Symbol)
    s === :set && return Base.Fix1(set!, a)
    getfield(a, s)
end

```

yielding

```julia
julia> a1 = A(4)
A{Int64}(4)

julia> a1.set(6)
6

julia> a1
A{Int64}(6)

```

I’d also recommend not doing the `set` thing with `getproperty` and just calling `set!` directly.

---

<div class="post-metadata">

**Author:** ![DerongXu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derongxu/32/23102_2.png) [@DerongXu](https://discourse.julialang.org/u/DerongXu)\
**Post date:** [October 3, 2022, 3:54am UTC](https://discourse.julialang.org/t/import-same-module-as-two-independent-identities/88132/3 "2022-10-03T03:54:51Z")

</div>

I am using the TaylorSeries package for Taylor polynomial expansions.

There is a type TaylorN in this package, which describes a polynomial with N variable. The number of variables and orders can be set via

```julia
using TaylorSeries
x, y = set_variables("x y", order=2);

```

However, I would like to obtain a composite type, like TaylorN{TaylorN{Float64}}. The outer TaylorN has different orders and number of variables with inner TaylorN.

Let me give you an example  
f(x,y)=a\_{00}+a\_{10}x+a\_{01}y+a\_{20}x^2+a\_{11}xy+a\_{02}y^2  
The coefficients are controlled by 3 knobs,  
a\_{ij}(k\_1,k\_2,k\_3)=c\_{000}+c\_{100}k\_1+c\_{010}k\_2+c\_{001}k\_3

The following code doesn’t work,

```julia
using TaylorSeries
x1,x2=set_variables("x1 x2",numvars=2,order=5)
y1,y2,y3=set_variables("y1 y2 y3",numvars=3,order=4) 
# x1 and x2 become invalid, because the above command 
# changes the maximum order and number of variables

```

Therefore, I wonder if there is a way so that

```julia
import TaylorSeries as TS1
import TaylorSeries as TS2
x1,x2=TS1.set_variables("x1 x2",numvars=2,order=5)
y1,y2,y3=TS2.set_variables("y1 y2 y3",numvars=3,order=4)
# typeof(x1)=TS1.TaylorN{Float64}
# typeof(y1)=TS2.TaylorN{Float64}
# both x1 and y1 are valid, and their types--- TS1.TalorN and TS2.TaylorN---are different 

```

I have a stupid method to solve this. But it seems weird.

```julia
module TS1
      include("path to TaylorSeries.jl")
end

module TS2
      include("path to TaylorSeries.jl")
end

# Now TaylorN in TS1 and TaylorN in TS2 are two different structs
# and they have their own method of set_variables 

```

---

<div class="post-metadata">

**Author:** ![lbenet](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@lbenet](https://discourse.julialang.org/u/lbenet)\
**Post date:** [October 8, 2022, 5:58pm UTC](https://discourse.julialang.org/t/import-same-module-as-two-independent-identities/88132/4 "2022-10-08T17:58:17Z")

</div>

Hi!

The way `TaylorSeries` works internally for the multivariable case, is by defining some internal tables which depend on the maximum degree of your polynomials and on the number of variables; those tables are some constant (but mutable) internal globals. That’s the reason that, if you run twice `set_variables` you overwrite the previous meaning of the maximum order and number of variables.

A possibility to achieve what you are after (including the variable order) is to use _nested_ `Taylor1` variables. So a polynomial of order two is a polynomial in one variable whose coefficients are polynomials in one variable, a similarly to more variables, as you describe above. While the possibility exists, we haven’t had the time to polish it and make it nicer for the user; see [WIP: Nested Taylors by blas-ko · Pull Request #126 · JuliaDiff/TaylorSeries.jl · GitHub](https://github.com/JuliaDiff/TaylorSeries.jl/pull/126). Moreover, we have doubts that this is as performant as the `TaylorN` approach. In any case, I am interested in your solution!

---

<div class="post-metadata">

**Author:** ![DerongXu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/derongxu/32/23102_2.png) [@DerongXu](https://discourse.julialang.org/u/DerongXu)\
**Post date:** [October 16, 2022, 2:16am UTC](https://discourse.julialang.org/t/import-same-module-as-two-independent-identities/88132/5 "2022-10-16T02:16:31Z")

</div>

Thanks for your information.
