Module functions logic depend on whether the third party library is used or not

Hi,

I’m pretty new in Julia.

My module MyModule consist of few functions and the behaviour of these functions may vary dependent whether the python package is installed or not (I’m going to use PyCall). I think I should introduce something like a global variable glob USE_PYTHON_LIB = true and within my functions I shuld implement something like:

module MyModule

try 
  SN = pyimport("sum_numbers")
  print("use python lib")
catch
  print("not use python lib")
end

function foo(x, y)
  if isdefined(MyModule, :SN)
    return = SN(x,y)
  end
x+y
end

end

Then when user executes using MyModule the SN variable should only be defined if the python module exist.

I think this should works but what is the best practice? Will the perfomance will critically suffer if I put isdefined() within loops in my functions?

You could do

foo = isdefined(MyModule, :SN) ? SN : (x,y)->x+y

and then no checking is performed inside loops

1 Like