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

1.905 visualizações
Pular para a primeira mensagem não lida

Marcos Sainz

não lida,
27 de jun. de 2013, 14:54:3127/06/2013
para 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

não lida,
27 de jun. de 2013, 17:37:2627/06/2013
para julia...@googlegroups.com
isinteractive() can be used. We don't really have a culture of doing this sort of thing, though.

-- John

Marcos Sainz

não lida,
27 de jun. de 2013, 21:02:4627/06/2013
para 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

não lida,
27 de jun. de 2013, 21:28:1827/06/2013
para 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

não lida,
27 de jun. de 2013, 21:43:0527/06/2013
para 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

não lida,
28 de jun. de 2013, 10:32:2528/06/2013
para 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

não lida,
28 de jun. de 2013, 11:19:5828/06/2013
para 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
Responder a todos
Responder ao autor
Encaminhar
0 nova mensagem