Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of python and julia Modules

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "python and julia module case analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "python and julia module case analysis"!

Module

Modules are workspaces that are isolated from each other and are similar to libraries in Python. When importing libraries in Python, use import * as *. In Julia, use using or import to import modules to be used.

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 (XBI y) = x + y

# minus function

My_minus (XBI y) = x-y

# multiply function

My_multiply (XBI y) = x * y

End

When you run using MyModule in REPL or in vscode (Atom), you will get the following error

Error prompted in REPL

Error prompted in vscode

This is because the location of the Module is not found in Julia, so we can add it manually and add push! (LOAD_PATH, ".") at the beginning of the program. Represents the current working directory, or you can set the. Change to an absolute directory; you can also use the cd dir command to change to our Module directory, and then directly use push! (LOAD_PATH, "."), so that you can call MyModule correctly.

If you don't want to write it in the program, you can also write it in the ~ / .Julia / config/startup.jl file, which indicates what needs to be done when the program is running. In Windows, ~ refers to the C:\ User\ UserName directory; ~ of MAC refers to the User\ UserName directory.

Export in MyModule derives these two functions so that you can use the my_square and my_abs functions directly without having to use MyModule.my_square and MyModule.my_abs

The code to invoke the module is 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 (3jue 4)

Res6 = MyModule.my_minus (4pm 3)

Res7 = my_minus (4pm 3) # error

We can also use the import MyModule statement, the basic usage is the same, if the declaration of export can not add the module name, then do not declare to add the module name, the difference is that if it is a function, using does not allow them to add new methods, can only use them, and import can not only be used, but also can add new methods.

Import MyModule

My_square (x::Float64) = x * x

Res = my_square (2.3)

Println (res)

Modules and files

Modules have nothing to do with files. A module can have multiple files, and a file can have multiple modules.

Multiple files in one module

Module MyModu

Include ("file1.jl")

Include ("file2.jl")

End

One file with multiple modules

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 top-level module, and when Julia starts, Main is set as the current module. The variables defined at the prompt go to Main, and executing varinfo () lists the variables in Main.

Core contains identifiers built into all languages (the core of the language, not libraries), 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 for most libraries, they are used.

At this point, I believe you have a deeper understanding of "python and julia module example analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report