9 modules

9 modules

Module

Modules are isolated workspaces. They are similar in usage to libraries in Python. When importing libraries in Python, import * as *the way they are used . In Julia, using or import is used to import the modules to be used.

The content of our module is as follows

module MyModule

export my_square, my_abs

# square function
my_square(x::Int64) = x * x

# abs function
my_abs(x) = (x>=0)? x: -x

# add function 
my_add(x,y) = x + y

# minus function 
my_minus(x,y) = x-y

# multiply function
my_multiply(x,y) = x * y

end

When running in the REPL or vscode (Atom), the using MyModuleerror will be prompted as follows

Error in REPL

Error prompt in vscode

This is because the location of the Module is not found in Julia, we can add it manually. At the very beginning of the program, add push!(LOAD_PATH, ".")to .indicate the current working directory. You can also change it .to an absolute directory; you can also use the first cd dircommand to switch to us The directory of the Module, and then use it directly push!(LOAD_PATH, "."), so that MyModule can be called correctly.

If you don't want to write it in the program, you can also write it in a ~/.julia/config/startup.jlfile, which specifies what operations need to be performed when the program is running. In Windows, it ~refers to the C:\User\UserNamedirectory; MAC ~refers to the User\UserNamedirectory.

In MyModule, exportthese two functions are exported , so that the my_squaresum my_absfunction can be used directly , instead of having to use MyModule.my_squareand MyModule.my_abs call the module code as follows

using MyModule
using MyModule:my_add, my_multiply
res1 = MyModule.my_square(2)
res2 = my_square(3)
res3 = my_abs(-4)
res5 = my_add(3,4)  
res6 = MyModule.my_minus(4,3)
res7 = my_minus(4,3) # error

We can also use import MyModulestatements. The basic usage is the same. If you declare exportit, you don’t need to add the module name . If you don’t declare it, you need to add the module name. The difference is that if it is a function, it is usingnot allowed to add new methods to them, but they can only be used. , importNot only can be used, but also new methods can be added to it.

import MyModule
my_square(x::Float64) = x * x
res = my_square(2.3)
println(res)

Modules and files

Modules and files are not related, a module can have multiple files, and a file can also have multiple modules

Multiple files per module

module MyModu

include("file1.jl")
include("file2.jl")

end

Multiple modules per file

module Test1
include("file1.jl")
end

module Test2
include("file2.jl")
end

Standard module

There are three very important standard modules: Main, Core and Base

Main is the topmost module. After Julia is started, Main will be set as the current module. Variables defined at the prompt will enter Main, and execution of varinfo() will list the variables in Main.

Core contains all the built-in identifiers of the language (the core part of the language, not the library), and each module declares using Core by default (otherwise, nothing can be done).

The Base module contains some basic functions (that is, the contents of the base/directory in the source code). All modules include using Base by default, because most libraries will use it.

Reference: https://cloud.tencent.com/developer/article/1653102 9 Module-Cloud + Community-Tencent Cloud