That's always been my feeling as well. I know this is the way things are
done in Python land, but I'd rather have modules be modules and scripts be
scripts, rather than mashing the two into a single file.


On Tue, Feb 11, 2014 at 4:55 PM, Spencer Russell <[email protected]> wrote:

> I've always found that feature of Python to be a little questionable
> anyways. it seems like unnecessary magic for some modules to have special
> behavior when executed.
>
>
> On Tue, Feb 11, 2014 at 8:48 AM, Mauro <[email protected]> wrote:
>
>> Sorry, I was wrong with the pointing out the correspondence of if
>> __name__ == '__main__': in python with something like `current_module()
>> == Main` in Julia.  Or at least wrong suggesting it can be used
>> similarly.
>>
>> In Python modules are tied to files and a module has the name of the
>> file.  So if you do `import matematicas`, python views the whole of the
>> file as the module matematicas and thus the test __name__ == '__main__'
>> fails.  Conversely, if you just run the file, python sees it as a script
>> and the test __name__ == '__main__' passes.
>>
>> In Julia however you have to specify the module separately inside the
>> file with `module Matematicas ... end`.  As you wrote it, your test
>> `if !isinteractive() && current_module() == Main`
>> is outside that module block and thus `current_module() == Main` will be
>> true and it gets executed.  However, if you put in inside the module
>> block it will never get executed!
>>
>> Bottom line is, I don't think there is an easy way in Julia to duplicate
>> that dual life of a file in python which will be a script if called
>> directly and a module if imported.  Have a look at some of the Julia
>> packages to see how they are organised, e.g.:
>> https://github.com/johnmyleswhite/Benchmark.jl
>> and try whether you can live with that.
>>
>> On Tue, 2014-02-11 at 06:01, [email protected] wrote:
>> > Hey I'm also getting used to git/github, is this a good bug report,
>> can't I
>> > reference the file Matematicas.jl or commit hash in better way when
>> opening
>> > issues?
>> >
>> > https://github.com/Ismael-VC/Club_TESCI/issues/1
>> >
>> > Wrapping the import/using/require/include code inside another enclosing
>> > moduleonly works as intended with "include":
>> >
>> > ismaelvc@toybox ~/T/C/C/julia_code (master)> cat > include_test.jl
>> > module IncludeTest
>> >
>> > include("Matematicas.jl")
>> >
>> > end
>> >
>> > ismaelvc@toybox ~/T/C/C/julia_code (master)> julia include_test.jl
>> > Control TEST!
>> >
>> >
>> >
>> > ismaelvc@toybox ~/T/C/C/julia_code (master)> cat > import_test.jl
>> > module ImportTest
>> >
>> > import Matematicas
>> >
>> > end
>> >
>> > ismaelvc@toybox ~/T/C/C/julia_code (master)> julia import_test.jl
>> > Test: Main
>> >
>> > fgen(4, 3, -1)
>> > x1 = 0.25
>> > x2 = -1.0
>> >
>> > Control TEST!
>> >
>> >
>> >
>> > ismaelvc@toybox ~/T/C/C/julia_code (master)> cat > using_test.jl
>> > module UsingTest
>> >
>> > using Matematicas
>> >
>> > end
>> >
>> > ismaelvc@toybox ~/T/C/C/julia_code (master)> julia using_test.jl
>> > Test: Main
>> >
>> > fgen(4, 3, -1)
>> > x1 = 0.25
>> > x2 = -1.0
>> >
>> > Control TEST!
>> >
>> >
>> >
>> > ismaelvc@toybox ~/T/C/C/julia_code (master)> cat > require_test.jl
>> > module RequireTest
>> >
>> > require("Matematicas.jl")
>> >
>> > end
>> > ismaelvc@toybox ~/T/C/C/julia_code (master)> julia require_test.jl
>> > Test: Main
>> >
>> > fgen(4, 3, -1)
>> > x1 = 0.25
>> > x2 = -1.0
>> >
>> > Control TEST!
>>
>
>

Reply via email to