What is the julia equivalent of pyton's if __name__ == '__main__':

1681 views
Skip to first unread message

Marcos Sainz

unread,
Jun 27, 2013, 2:54:31 PM6/27/13
to julia...@googlegroups.com

In python there's the convention of using

if __name__ == '__main__':
  # do something

to add "executable" behavior to a file which may otherwise be included into another module or class.

Is there something analogous in julia? 
I tried using

if current_module() == Main
  # do something
end

but this does not quite do the same.

Thank you.

Marcos

John Myles White

unread,
Jun 27, 2013, 5:37:26 PM6/27/13
to julia...@googlegroups.com
isinteractive() can be used. We don't really have a culture of doing this sort of thing, though.

-- John

Marcos Sainz

unread,
Jun 27, 2013, 9:02:46 PM6/27/13
to julia...@googlegroups.com
Thank you John. You got me curious with the comment about culture. Could you develop a little further on it and/or provide some references?
I've been looking at lots of sample code, packages, and julia core itself to indeed try to "distil" some "culture" aspects of the language, recurring patterns, "julia-isms".

Stefan Karpinski

unread,
Jun 27, 2013, 9:28:18 PM6/27/13
to julia...@googlegroups.com
I feel that files are either code to be loaded by something else or code to run. Python likes to mix the two with that idiom but I've never really seen the point. It just seems like kind of an anti-pattern to me.

John Myles White

unread,
Jun 27, 2013, 9:43:05 PM6/27/13
to julia...@googlegroups.com
My impression is that Python sometimes uses this advice as a quick-and-dirty form of unit testing. I think we'd generally just make a separate file full of @asserts.

 -- John

nathan hodas

unread,
Jun 28, 2013, 10:32:25 AM6/28/13
to julia...@googlegroups.com
Python also requires '__main__' when utilizing multiprocessing (http://docs.python.org/2/library/multiprocessing.html), presumably so child processes can load global code without getting caught in infinite recursions.   

John Myles White

unread,
Jun 28, 2013, 11:19:58 AM6/28/13
to julia...@googlegroups.com
I think the culture of Julia is still being developed, but most of the larger packages provide a good insight.

Jeff also started writing a style guide, which helps: http://docs.julialang.org/en/latest/manual/style-guide/

I've written up some notes of my own and will write up a blog post soon.

 -- John
Reply all
Reply to author
Forward
0 new messages