On Mon, Nov 4, 2019 at 3:16 PM <jf...@ms4.hinet.net> wrote: > > Chris Angelico於 2019年11月4日星期一 UTC+8上午10時19分50秒寫道: > > On Mon, Nov 4, 2019 at 1:01 PM <jf...@ms4.hinet.net> wrote: > > > > > > Chris Angelico於 2019年11月4日星期一 UTC+8上午8時43分07秒寫道: > > > > Ah, that's a fair point. If you specifically WANT that behaviour, what > > > > you can do is invoke the script interactively: > > > > > > > > python3 -i test.py > > > > > > > > That'll run the script as normal, and then drop you into the REPL. All > > > > your interactive globals *are* that module's globals. > > > > > > > > ChrisA > > > > > > It surprises me that REPL has essential different behavior in these two > > > situations. > > > > > > > Not really. In each case, the REPL lets you interactively execute code > > as part of a module. If you start with "-i somescript.py", it starts > > out by running the contents of that script; otherwise, you start with > > nothing (as if you ran "-i empty-file.py"). The REPL does the same > > thing every time; it's a difference between creating the functions > > directly and importing them. > > > > ChrisA > > I mean, taking this simple example: > ---test.py--- > def main(): > print(rule) > if __name__ == '__main__: > rule = 1 > main() > --- > > case 1: > py -i test.py > 1 > >>> globals() > >>> main.__globals__ > > case 2: > py > >>> from test import * > >>> globals() > >>> main.__globals__ > > The result is much different. In case 1, the REPL and the module seems in the > same global space:-) >
Yes. The difference is that one of them uses "import" and the other does not. ChrisA -- https://mail.python.org/mailman/listinfo/python-list