seeker
seeker

Reputation: 704

julia counterpart for "from module import some_func" in python

I find using Module in julia is equivalent to from Module import * in python. Is there any way to import a single function or variable from a module.

Upvotes: 1

Views: 299

Answers (2)

Toivo Henningsson
Toivo Henningsson

Reputation: 2699

You can import e.g. a single function with

using Module.func

If you also want to be able to extend the function with new methods, use instead

import Module.func

If you want to import several named functions and variables from the same module, you can e.g. use one of

using Module: func1, func2
import Module: func1, func2

All of these forms allow to import definitions from Module regardless of whether they are exported there, unlike using Module, which only imports exported definitions.

Also keep in mind that it is really values that are imported, not variables, so if you try to assign to an imported variable, you will not see a change in the originating module (and you will get a warning).

Upvotes: 5

Mr Alpha
Mr Alpha

Reputation: 1843

If you have a module Foo with a function bar (or something else specific you wan to import) you can do:

import Foo: bar

This brings only bar into scope.

Upvotes: 1

Related Questions