What's the difference between running a script under command box and interpreter?
jfong at ms4.hinet.net
jfong at ms4.hinet.net
Fri Nov 1 07:24:38 EDT 2019
Cameron Simpson於 2019年11月1日星期五 UTC+8下午5時28分42秒寫道:
> On 31Oct2019 22:03, Jach Fong <jfong at ms4.hinet.net> wrote:
> >Cameron Simpson於 2019年11月1日星期五 UTC+8下午12時13分45秒寫道:
> >> On 31Oct2019 20:44, Jach Fong <jfong at ms4.hinet.net> wrote:
> >> >The script test.py is something like this:
> >> >-------test.py
> >> >from pyeds import fsm
> >> >...
> >> >class Rule_Parse:
> >> > def __init__(self):
> >> > ...
> >> > self.current_char = ''
> >> >...
> >> >def main(input_str):
> >> > for c in input_str:
> >> > ...
> >> > rule.current_char = c
> >> > ...
> >> >
> >> >if __name__ == '__main__':
> >> > input_str = '(NNS(acoustics) & RB(not)) | JJ(muted)'
> >> > rule = Rule_Parse()
> >> > main(input_str)
> >> > ...
> >> >
> >> >-----------
> >> >The test.py can run correctly under command box:
> >> >D:\Works\Python\pyeds-master\src>py test.py
> >> >
> >> >but fails when running under interpreter:
> >> >D:\Works\Python\pyeds-master\src>py
> >> >Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] on win32
> >> >Type "help", "copyright", "credits" or "license" for more information.
> >> >>>> from test import *
> >> >>>> input_str = "(NNS(acoustics) & RB(not)) | JJ(muted)"
> >> >>>> rule = Rule_Parse()
> >> >>>> main(input_str)
> >> >Traceback (most recent call last):
> >> > File "<stdin>", line 1, in <module>
> >> > File "D:\Works\Python\pyeds-master\src\test.py", line 229, in main
> >> > rule.current_char = c
> >> >NameError: name 'rule' is not defined
> >> >>>>
> >> >
> >> >I did check the globals using dir() and the 'rule' is there, no doubt.
> >>
> >> It matters how you checked this. This isn't apparent.
> [...]
> >Yes, the 'if' body is not executed when I import test.py under
> >interpreter, that's why I manually execute them there.
> >What puzzles me is that the globals() has a 'rule' object in both
> >cases. Why this one doesn't work?
>
> I think I have misinterpreted what you've done.
>
> The globals are your current module's namespace, and functions defines
> in a module are bound to that module's namespace.
>
> Strip your test.py back. A lot. Try this:
>
> def main():
> print(rule)
>
> Now, let's use that:
>
> Python 3.7.4 (default, Sep 28 2019, 13:34:38)
> [Clang 8.0.0 (clang-800.0.42.1)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import test
> >>> test.main()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/Users/cameron/tmp/d1/test.py", line 2, in main
> print(rule)
> NameError: name 'rule' is not defined
>
> What's happening here?
>
> When we call main it tries to print "rule" from its module's globals.
>
> The first time you call it that doesn't exist, and we get your error.
>
> Setting rule=1 in the interpreter's space doesn't help (the stuff below
> if from the same session continued from above):
>
> >>> rule=1
> >>> test.main()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/Users/cameron/tmp/d1/test.py", line 2, in main
> print(rule)
> NameError: name 'rule' is not defined
>
> But if we define rule in the "test" module things improve:
>
> >>> test.rule=2
> >>> test.main()
> 2
>
> Importing main from test doesn't change where it looks for its globals:
>
> >>> from test import main as my_main
> >>> my_main()
> 2
>
> That value (2) is still coming out of the test module.
>
> Cheers,
> Cameron Simpson <cs at cskk.id.au>
I didn't noticed that the interpreter has its own globals. Thanks for reminding.
--Jach
More information about the Python-list
mailing list