change of random state when pyc created??
Alan Isaac
aisaac at american.edu
Sat May 5 20:20:04 EDT 2007
"John Machin" <sjmachin at lexicon.net> wrote in message
news:1178408359.113807.181570 at l77g2000hsb.googlegroups.com...
> (a) module1 imports random and (MyClass from module2)
Right.
> It's a bit of a worry that you call the first file "module1" and not
> "the_script". Does module2 import module1, directly or indirectly?
No.
I call a module any file meant to be imported by others.
Many of my modules include a "main" function,
which allow the module to be executed as a script.
I do not think this is unusual, even as terminology.
> Should you not expect to get the same result each time? Is that not
> the point of setting a constant seed each time you run the script?
Yes. That is the problem.
If I delete module2.pyc,
I do not get the same result.
> With all due respect to your powers of description :-) no, it can't be
> explained properly, without seeing the contents of the source files.
I sent them to you.
What behavior did you see?
> from random import seed
> seed(314)
> class Trivial:
> pass
> ===
> Is module2 ... doing that?
> Is module1 importing itself (directly or indirectly)?
No.
Separate issue
==============
> Here's a suggestion for how you should structure scripts:
>
> def main():
> # All productive code is inside a function to take advantage
> # of access to locals being faster than access to globals
> import mymodule
> mymodule.do_something()
> if __name__ == "__main__":
> main()
> else:
> raise Exception("Attempt to import script containing nothing
> importable")
>
> and your modules should *start* with:
> if __name__ == "__main__":
> raise Exception("Attempt to execute hopefully-pure module as a
> script")
I'm not going to call this a bad practice, since it has clear virtues.
I will say that it does not seem to be a common practice, although that
may be my lack of exposure to other's code. And it still does not
address the common need of playing with a "package in progress"
or a "package under consideration" without installing it.
Cheers,
Alan Isaac
More information about the Python-list
mailing list