aneccodeal
aneccodeal

Reputation: 8923

Module aliasing in Julia

In python you can do something like this to let you use a shortened module name:

>>> import tensorflow as tf

From then on you can refer to tf, instead of having to type tensorflow everywhere.

Is something like this possible in Juila?

Upvotes: 17

Views: 5041

Answers (3)

Alex
Alex

Reputation: 2434

I'm pushing the practical snippet @Alan don't mentioned:

  • At MyMod.jl:
module MyModule

plus(x, y) = x + y
myfield = 0

end
  • At Main.jl:
# Only if you're including the module from another file.
include("MyMod.jl")

import .MyModule as mymod

println(mymod.myfield)
println(mymod.plus(1, 1))

Main resources:

Upvotes: 2

Alan
Alan

Reputation: 9620

Julia now supports renaming with as.

Upvotes: 9

Fengyang Wang
Fengyang Wang

Reputation: 12061

Yep, you can just assign the module to a new name.

import JSON
const J = JSON

J.print(Dict("Hello, " => "World!"))

I highly recommend to use the const because otherwise there will be a performance penalty. (With the const, there is no performance penalty.)

Upvotes: 26

Related Questions