<div class="gmail_quote">On Wed, Nov 16, 2011 at 1:21 PM, Eric Snow <span dir="ltr"><<a href="mailto:ericsnowcurrently@gmail.com">ericsnowcurrently@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">But which is more astonishing (POLA and all that): running your module</div></div>
in Python, it behaves differently than when you import it (especially<br>
__name__); or you add an __init__.py to a directory and your *scripts*<br>
there start to behave differently?<br></blockquote><div><br></div><div>To me it seems that the latter is more astonishing because there's less connection between your action and the result. If you're running something differently, it makes more sense that it acts differently, because you've changed what you're *doing*. In the scripts case, you haven't changed how you run the scripts, and you haven't changed the scripts, so the change in behavior seems to appear out of nowhere.</div>
<div> </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">When I was learning Python, it took quite a while before I realized<br>
that modules are imported and scripts are passed at the commandline;<br>
and to understand the whole __main__ thing.</blockquote><div><br></div><div>It doesn't seem to me that PEP 395 fixes this problem. In order to *actually* fix it, we'd need to have some sort of "package" statement like in other languages - then you'd declare right there in the code what package it's supposed to be part of.</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> It has always been a pain, particularly when I wanted to<br>
just check a module really quickly for errors.<br></blockquote><div><br></div><div>What, specifically, was a pain? That information might be of more use in determining a solution.</div><div><br></div><div>If you mean that you had other modules importing the module that was also __main__, then I agree that having a solution for __main__-aliasing is a good idea. I just think it might be more cleanly fixed by checking whether the __file__ of a to-be-imported module is going to end up matching __main__.__file__, and if so, alias __main__ instead.</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">However, lately I've actually taken to the idea that it's better to<br>
write a test script that imports the module and running that, rather<br>
than running the module itself. But that came with the understanding<br>
that the module doesn't go through the import machinery when you *run*<br>
it, which I don't think is obvious, particularly to beginners. So<br>
Nick's solution, to me, is an appropriate concession to the reality<br>
that most folks will expect Python to treat their modules like modules<br>
and their scripts like scripts.<br></blockquote><div><br></div><div>You lost me there: if most people don't understand the difference, then why are they expecting a difference?</div><div><br></div></div>