Import without executing module
Ray
raykyoto at gmail.com
Tue Feb 3 21:53:53 EST 2009
Hi Lie,
On Feb 3, 7:21 pm, Lie <Lie.1... at gmail.com> wrote:
> On Feb 3, 1:37 pm, Ray <rayky... at gmail.com> wrote:
>
> > I'll enclose the top-level commands with the if statement above...its
> > just a minor change, but it seems unavoidable.
>
> > Thanks again!
>
> > Ray
>
> If you really don't want the file to be changed, you could (depends on
> the module) use the module as a subprocess. The ideal solution is for
> the module to have an if __name__ == '__main__': to determine whether
> it is being used as a module or a standalone program though.
Thank you for this! I've done the "if __name__ ..." as you and others
suggested. It isn't what I would have liked, but I'm ok with
it...especially after finding out that top-level code has to be run
while importing. As I'm new to python, I didn't know about that, but
I now see there's no way around it.
So, is inserting the above if statement common practice for python
programmers? As a C programmer, it seems that people put a "#ifndef
XXX...#define XXX...[all of the code]...#endif" almost as a habit. I
wonder if its the same thing?
Thank you!
Ray
More information about the Python-list
mailing list