Re: [Tutor] Which is better Practice and why

2012-10-23 Thread Oscar Benjamin
On 23 October 2012 02:17, Devin Jeanpierre wrote: > On Mon, Oct 22, 2012 at 8:42 PM, Steven D'Aprano wrote: >> If you do that, and the module directly or indirectly imports itself >> while it is running as a script, you may run into trouble. But writing >> a dual-purpose module that is usable as

Re: [Tutor] Which is better Practice and why

2012-10-23 Thread Devin Jeanpierre
On Tue, Oct 23, 2012 at 5:59 AM, Steven D'Aprano wrote: > I think you'll find that you're wrong there :) Oops. >> If I want to test something, I put it in another file. > Yes. And how do you import the __main__.py module to test it, without > it executing? How do you test the body of code in th

Re: [Tutor] Which is better Practice and why

2012-10-23 Thread Steven D'Aprano
On 23/10/12 12:53, Devin Jeanpierre wrote: On Mon, Oct 22, 2012 at 8:54 PM, Steven D'Aprano wrote: package.__main__.py is designed to be run as a script, and not to be imported. But that doesn't mean that there's no good purpose for importing it. If your package is non-trivial, you ought to h

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Devin Jeanpierre
On Mon, Oct 22, 2012 at 8:54 PM, Steven D'Aprano wrote: >> Recently I've become a fan of executable packages. In __main__.py, >> it's always the right thing to do. > > > I would disagree there too. I think that unconditionally running main > is the wrong thing to do, except perhaps in the most tri

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Steven D'Aprano
On 23/10/12 11:25, Devin Jeanpierre wrote: On Mon, Oct 22, 2012 at 6:15 PM, Steven D'Aprano wrote: Not that. That unconditionally executes main the first time you access the module, *regardless* of whether it is being run as a script or being imported. That is nearly always the wrong thing to d

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Devin Jeanpierre
On Mon, Oct 22, 2012 at 8:42 PM, Steven D'Aprano wrote: > If you do that, and the module directly or indirectly imports itself > while it is running as a script, you may run into trouble. But writing > a dual-purpose module that is usable as an importable module or as a > stand-alone script is not

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Steven D'Aprano
On 22/10/12 22:54, Devin Jeanpierre wrote: On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: the 2nd one usually includes a lot more code then i showed. can you please tell me why different methods are used to access the main code? is it just preference or is one way actually better coding

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Devin Jeanpierre
On Mon, Oct 22, 2012 at 9:23 AM, Walter Prins wrote: > Devin, > > On 22 October 2012 12:54, Devin Jeanpierre wrote: >> On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: >>> the 2nd one usually includes a lot more code then i showed. can you please >>> tell me why different methods are used t

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Devin Jeanpierre
On Mon, Oct 22, 2012 at 6:15 PM, Steven D'Aprano wrote: > Not that. That unconditionally executes main the first time you access > the module, *regardless* of whether it is being run as a script or > being imported. That is nearly always the wrong thing to do. Recently I've become a fan of execut

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Steven D'Aprano
On 22/10/12 22:45, Matthew Ngaha wrote: In many of the tutorial examples ive come across, the main code's program is never at the top level, but always in a function of some sort. i understand why but, there is always a different way used to access the main code, i want to know which is the best.

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread eryksun
On Mon, Oct 22, 2012 at 2:26 PM, Oscar Benjamin wrote: > > They both import each other. That is a circular import and it can > create problems. My advice for this issue is not "avoid importable > scripts" but rather "avoid circular imports" (a good idea anyway). I agree it's better to refactor co

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Oscar Benjamin
On 22 October 2012 16:14, eryksun wrote: > On Mon, Oct 22, 2012 at 9:35 AM, Oscar Benjamin > wrote: >> >> It is also sometimes useful to define a number of scripts that are in >> the same directory but share some code by importing one another. You >> need the if __name__ =="__main__" block for th

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread eryksun
On Mon, Oct 22, 2012 at 11:14 AM, eryksun wrote: > > Just to clarify that I'm following you, would you count the following > as a script importing itself 'indirectly'? > > Assume two modules in the same directory, mod1.py and mod2.py, can > both act as the main entry point, and both import each ot

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread eryksun
On Mon, Oct 22, 2012 at 9:35 AM, Oscar Benjamin wrote: > > It is also sometimes useful to define a number of scripts that are in > the same directory but share some code by importing one another. You > need the if __name__ =="__main__" block for this. > > The problem that Devin is referring to onl

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Oscar Benjamin
On 22 October 2012 12:54, Devin Jeanpierre wrote: > On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: Hi Devin, your context was missing the crucial part showing the "2nd one": >> they call the main program by simply calling the main function. I've also >> seen a more complcated: >> >> if _

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Walter Prins
Devin, On 22 October 2012 12:54, Devin Jeanpierre wrote: > On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: >> the 2nd one usually includes a lot more code then i showed. can you please >> tell me why different methods are used to access the main code? is it just >> preference or is one way

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread eryksun
On Mon, Oct 22, 2012 at 7:54 AM, Devin Jeanpierre wrote: > > The second one is used if your importable modules are also scripts > that can be executed. > > That's a bad idea, because it results in the same source file being > executed twice, producing distinct classes that break typechecking. Her

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Matthew Ngaha
oh ok i understand it.. Thanks for the help guys ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Joel Goldstick
On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: > In many of the tutorial examples ive come across, the main code's program is > never at the top level, but always in a function of some sort. i understand > why but, there is always a different way used to access the main code, i > want to kn

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Mark Lawrence
On 22/10/2012 12:45, Matthew Ngaha wrote: In many of the tutorial examples ive come across, the main code's program is never at the top level, but always in a function of some sort. i understand why but, there is always a different way used to access the main code, i want to know which is the bes

Re: [Tutor] Which is better Practice and why

2012-10-22 Thread Devin Jeanpierre
On Mon, Oct 22, 2012 at 7:45 AM, Matthew Ngaha wrote: > the 2nd one usually includes a lot more code then i showed. can you please > tell me why different methods are used to access the main code? is it just > preference or is one way actually better coding practice? The second one is used if you

[Tutor] Which is better Practice and why

2012-10-22 Thread Matthew Ngaha
In many of the tutorial examples ive come across, the main code's program is never at the top level, but always in a function of some sort. i understand why but, there is always a different way used to access the main code, i want to know which is the best. main() main's code #top level mai