Re: What's the difference between running a script under command box and interpreter?
Grant Edwards於 2019年11月5日星期二 UTC+8上午12時41分24秒寫道: > On 2019-11-04, Dennis Lee Bieber wrote: > > Using > > > >from module import * > > > > is often the worst thing one can do. > > I agree 100%. > > Unfortunately, most of the official standard library documentation is > written assuming you do _exactly_that_. > > Though it makes the documetnation more succinct, I think it encourages > bad behavior and causes a lot of problems. > > -- > Grant Edwards grant.b.edwardsYow! I wonder if I could > at ever get started in the > gmail.comcredit world? I don't use this phrase in my files, but sometime I use it under REPL. I don't think it's bad showing in the document, at least it will teach someone who was bitten by this phrase something unique in Python:-) --Jach -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
On 2019-11-04 21:05, Peter J. Holzer wrote: On 2019-11-03 16:34:39 -0800, jf...@ms4.hinet.net wrote: I innocently thought that when import module through "from test import *", I am working on test's globals under REPL. I didn't noticed the REPL has its own globals. Well, you imported every global from test. So you are (kind of) working on those globals, or at least the objects they are referencing. In the real world, when you import a car from Germany, the car is now in your country, but you are not in Germany. Even if you import all the cars from Germany, you are still not in Germany. It's the same way in Python. Well, kind of. One important difference is that when you import a car from Germany, that car is no longer in Germany. But when you import a symbol from a Python module, that symbol still exists in that module. You just have a second symbol in your namespace referencing the same object (as Cameron pointed out). The car itself isn't in Germany or your own country, it's just "somewhere". All you've imported is a (copy of a) reference to that car, under some name, and there can be other references to it in other places too, possibly under the same name, possibly under a different name. -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
On 2019-11-03 16:34:39 -0800, jf...@ms4.hinet.net wrote: > I innocently thought that when import module through "from test import > *", I am working on test's globals under REPL. I didn't noticed the > REPL has its own globals. Well, you imported every global from test. So you are (kind of) working on those globals, or at least the objects they are referencing. In the real world, when you import a car from Germany, the car is now in your country, but you are not in Germany. Even if you import all the cars from Germany, you are still not in Germany. It's the same way in Python. Well, kind of. One important difference is that when you import a car from Germany, that car is no longer in Germany. But when you import a symbol from a Python module, that symbol still exists in that module. You just have a second symbol in your namespace referencing the same object (as Cameron pointed out). hp -- _ | Peter J. Holzer| Story must make more sense than reality. |_|_) || | | | h...@hjp.at |-- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
On 2019-11-04, Dennis Lee Bieber wrote: > Using > >from module import * > > is often the worst thing one can do. I agree 100%. Unfortunately, most of the official standard library documentation is written assuming you do _exactly_that_. Though it makes the documetnation more succinct, I think it encourages bad behavior and causes a lot of problems. -- Grant Edwards grant.b.edwardsYow! I wonder if I could at ever get started in the gmail.comcredit world? -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
On Mon, Nov 4, 2019 at 3:16 PM wrote: > > Chris Angelico於 2019年11月4日星期一 UTC+8上午10時19分50秒寫道: > > On Mon, Nov 4, 2019 at 1:01 PM 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
Re: What's the difference between running a script under command box and interpreter?
Chris Angelico於 2019年11月4日星期一 UTC+8上午10時19分50秒寫道: > On Mon, Nov 4, 2019 at 1:01 PM 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:-) --Jach -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
On Mon, Nov 4, 2019 at 1:01 PM 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
Chris Angelico於 2019年11月4日星期一 UTC+8上午8時43分07秒寫道: > On Mon, Nov 4, 2019 at 11:36 AM wrote: > > > > Peter J. Holzer於 2019年11月4日星期一 UTC+8上午3時59分36秒寫道: > > > On 2019-11-01 04:24:38 -0700, jf...@ms4.hinet.net wrote: > > > > > 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 "", line 1, in > > > > > File "/Users/cameron/tmp/d1/test.py", line 2, in main > > > > > print(rule) > > > > > NameError: name 'rule' is not defined > > > > > > [Explanation snipped] > > > > > > > I didn't noticed that the interpreter has its own globals. Thanks for > > > > reminding. > > > > > > It's not really "the interpreter" (I think you mean the REPL) which has > > > it's own globals. Every module/file has its own globals. > > > > > > The same thing happens non-interactively: > > > > > > % cat test.py > > > def main(): > > > print(rule) > > > > > > % cat foo.py > > > #!/usr/bin/python3 > > > > > > from test import * > > > > > > rule = 42 > > > main() > > > > > > % ./foo.py > > > Traceback (most recent call last): > > > File "./foo.py", line 6, in > > > main() > > > File "/home/hjp/tmp/test.py", line 2, in main > > > print(rule) > > > NameError: name 'rule' is not defined > > > > > > The "rule" identifier in main() refers to a "rule" variable in the > > > module test. If you set a variable "rule" somewhere else (in foo.py or > > > the REPL, ...), that has no effect. How should python know that you want > > > to set the rule variable in the test module? > > > > > > hp > > > > > > -- > > >_ | Peter J. Holzer| Story must make more sense than reality. > > > |_|_) || > > > | | | h...@hjp.at |-- Charles Stross, "Creative writing > > > __/ | http://www.hjp.at/ | challenge!" > > > > > > I innocently thought that when import module through "from test import *", > > I am working on test's globals under REPL. I didn't noticed the REPL has > > its own globals. > > > > 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. --Jach -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
On 03Nov2019 16:34, Jach Fong wrote: Peter J. Holzer於 2019年11月4日星期一 UTC+8上午3時59分36秒寫道: It's not really "the interpreter" (I think you mean the REPL) which has it's own globals. Every module/file has its own globals. The same thing happens non-interactively: % cat test.py def main(): print(rule) % cat foo.py #!/usr/bin/python3 from test import * rule = 42 main() % ./foo.py Traceback (most recent call last): File "./foo.py", line 6, in main() File "/home/hjp/tmp/test.py", line 2, in main print(rule) NameError: name 'rule' is not defined The "rule" identifier in main() refers to a "rule" variable in the module test. If you set a variable "rule" somewhere else (in foo.py or the REPL, ...), that has no effect. How should python know that you want to set the rule variable in the test module? [...] I innocently thought that when import module through "from test import *", I am working on test's globals under REPL. I didn't noticed the REPL has its own globals. Aye. An import statement is essentially a funny shaped assignment statement (aside from the side effect of loading the required module). When you go: from blah import foo You're getting a _local_ variable "foo" which references the same _value_ that "blah.foo" also references. But it is independent of "blah.foo"; assigning to it (changing what it references) does not change what "blah.foo" references. To take a concrete example, I've a tiny module "cs.x" which essentially supplies just a single function X() whose purpose it to write a debug message (no logging modules or other complications). So lots of my dev code has (while debugging): from cs.x import X and then: X("some message about %s", variable_name) X() normally just writes to sys.stderr, but it has some module level mode switches such X_via_tty which literally opens "/dev/tty" and writes to that, invented for situations where sys.stderr has been intercepted. Occasionally I need to set that mode (usually in a unit test I'm debugging). So I go: from cs.x import X # as normal import cs.x; cs.x.X_via_tty = True If I went: from cs.x import X, X_via_tty X_via_tty = True it wouldn't work. How should python know that you want to set the rule variable in the test module? My 'wrong' answer will be, at the time I raised my question, that when import two different modules either has 'rule' variable, REPL will see the second imported one. No kidding:-) Well: from mod1 import rule from mod2 import rule is like: import mod1 import mod2 rule = mod1.rule# copy reference rule = mod2.rule# copy the other reference Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
On Mon, Nov 4, 2019 at 11:36 AM wrote: > > Peter J. Holzer於 2019年11月4日星期一 UTC+8上午3時59分36秒寫道: > > On 2019-11-01 04:24:38 -0700, jf...@ms4.hinet.net wrote: > > > > 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 "", line 1, in > > > > File "/Users/cameron/tmp/d1/test.py", line 2, in main > > > > print(rule) > > > > NameError: name 'rule' is not defined > > > > [Explanation snipped] > > > > > I didn't noticed that the interpreter has its own globals. Thanks for > > > reminding. > > > > It's not really "the interpreter" (I think you mean the REPL) which has > > it's own globals. Every module/file has its own globals. > > > > The same thing happens non-interactively: > > > > % cat test.py > > def main(): > > print(rule) > > > > % cat foo.py > > #!/usr/bin/python3 > > > > from test import * > > > > rule = 42 > > main() > > > > % ./foo.py > > Traceback (most recent call last): > > File "./foo.py", line 6, in > > main() > > File "/home/hjp/tmp/test.py", line 2, in main > > print(rule) > > NameError: name 'rule' is not defined > > > > The "rule" identifier in main() refers to a "rule" variable in the > > module test. If you set a variable "rule" somewhere else (in foo.py or > > the REPL, ...), that has no effect. How should python know that you want > > to set the rule variable in the test module? > > > > hp > > > > -- > >_ | Peter J. Holzer| Story must make more sense than reality. > > |_|_) || > > | | | h...@hjp.at |-- Charles Stross, "Creative writing > > __/ | http://www.hjp.at/ | challenge!" > > > I innocently thought that when import module through "from test import *", I > am working on test's globals under REPL. I didn't noticed the REPL has its > own globals. > 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
Peter J. Holzer於 2019年11月4日星期一 UTC+8上午3時59分36秒寫道: > On 2019-11-01 04:24:38 -0700, jf...@ms4.hinet.net wrote: > > > 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 "", line 1, in > > > File "/Users/cameron/tmp/d1/test.py", line 2, in main > > > print(rule) > > > NameError: name 'rule' is not defined > > [Explanation snipped] > > > I didn't noticed that the interpreter has its own globals. Thanks for > > reminding. > > It's not really "the interpreter" (I think you mean the REPL) which has > it's own globals. Every module/file has its own globals. > > The same thing happens non-interactively: > > % cat test.py > def main(): > print(rule) > > % cat foo.py > #!/usr/bin/python3 > > from test import * > > rule = 42 > main() > > % ./foo.py > Traceback (most recent call last): > File "./foo.py", line 6, in > main() > File "/home/hjp/tmp/test.py", line 2, in main > print(rule) > NameError: name 'rule' is not defined > > The "rule" identifier in main() refers to a "rule" variable in the > module test. If you set a variable "rule" somewhere else (in foo.py or > the REPL, ...), that has no effect. How should python know that you want > to set the rule variable in the test module? > > hp > > -- >_ | Peter J. Holzer| Story must make more sense than reality. > |_|_) || > | | | h...@hjp.at |-- Charles Stross, "Creative writing > __/ | http://www.hjp.at/ | challenge!" I innocently thought that when import module through "from test import *", I am working on test's globals under REPL. I didn't noticed the REPL has its own globals. >>> How should python know that you want to set the rule variable in the test >>> module? My 'wrong' answer will be, at the time I raised my question, that when import two different modules either has 'rule' variable, REPL will see the second imported one. No kidding:-) --Jach -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
On 2019-11-01 04:24:38 -0700, jf...@ms4.hinet.net wrote: > > 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 "", line 1, in > > File "/Users/cameron/tmp/d1/test.py", line 2, in main > > print(rule) > > NameError: name 'rule' is not defined [Explanation snipped] > I didn't noticed that the interpreter has its own globals. Thanks for > reminding. It's not really "the interpreter" (I think you mean the REPL) which has it's own globals. Every module/file has its own globals. The same thing happens non-interactively: % cat test.py def main(): print(rule) % cat foo.py #!/usr/bin/python3 from test import * rule = 42 main() % ./foo.py Traceback (most recent call last): File "./foo.py", line 6, in main() File "/home/hjp/tmp/test.py", line 2, in main print(rule) NameError: name 'rule' is not defined The "rule" identifier in main() refers to a "rule" variable in the module test. If you set a variable "rule" somewhere else (in foo.py or the REPL, ...), that has no effect. How should python know that you want to set the rule variable in the test module? hp -- _ | Peter J. Holzer| Story must make more sense than reality. |_|_) || | | | h...@hjp.at |-- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" signature.asc Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
Cameron Simpson於 2019年11月1日星期五 UTC+8下午5時28分42秒寫道: > On 31Oct2019 22:03, Jach Fong wrote: > >Cameron Simpson於 2019年11月1日星期五 UTC+8下午12時13分45秒寫道: > >> On 31Oct2019 20:44, Jach Fong 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 "", line 1, in > >> > 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 "", line 1, in > 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 "", line 1, in > 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 I didn't noticed that the interpreter has its own globals. Thanks for reminding. --Jach -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
On 31Oct2019 22:03, Jach Fong wrote: Cameron Simpson於 2019年11月1日星期五 UTC+8下午12時13分45秒寫道: On 31Oct2019 20:44, Jach Fong 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 "", line 1, in > 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 "", line 1, in 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 "", line 1, in 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
Cameron Simpson於 2019年11月1日星期五 UTC+8下午12時13分45秒寫道: > On 31Oct2019 20:44, Jach Fong 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 "", line 1, in > > 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. > > >I also tried "py -m pdb test.py" and step through it, no problem too. > > This: > py test.py > and this: > py -m pdb test > > both run test.py with __name__ set to "__main__" to indicate that > test.py is the main programme. > > When you "import test", the module's __name__ is from the import > ("test") i.e. not the main programme. > > The bottom of your module has an if statement whose body only runs when > this is the main programme. > > The core issue is that the global "rule" is _only_ defined inside that > if statement. > > You might set it unconditionally to None at the start of the file, but > that would only change the failure mode. > > You might set it unconditionally to Rule_Parse() at the top of the file > but that pointlessly instantiates an instance of Rule_Parse which might > never be needed (maybe that is cheap, but many other classes are not). > The basic intent of an import is to define various classes and other > names, but _not_, generally, to create class instances and do > significant work. > > This is really an example illustrating one reason why global variables > are considered things to avoid almost all of the time. Had main() > required "rule" as a parameter then it would not have been possible to > call it without at least providing a rule. The same applies with most > other functions: if they all require their external state via parameters > then you can't miss things out. (You can, of course, always pass > incorrect values, but that is usually easier to debug.) > > Avoid globals; they are usually a source of bugs. > > Cheers, > Cameron Simpson 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? --Jach -- https://mail.python.org/mailman/listinfo/python-list
Re: What's the difference between running a script under command box and interpreter?
On 31Oct2019 20:44, Jach Fong 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 "", line 1, in 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. I also tried "py -m pdb test.py" and step through it, no problem too. This: py test.py and this: py -m pdb test both run test.py with __name__ set to "__main__" to indicate that test.py is the main programme. When you "import test", the module's __name__ is from the import ("test") i.e. not the main programme. The bottom of your module has an if statement whose body only runs when this is the main programme. The core issue is that the global "rule" is _only_ defined inside that if statement. You might set it unconditionally to None at the start of the file, but that would only change the failure mode. You might set it unconditionally to Rule_Parse() at the top of the file but that pointlessly instantiates an instance of Rule_Parse which might never be needed (maybe that is cheap, but many other classes are not). The basic intent of an import is to define various classes and other names, but _not_, generally, to create class instances and do significant work. This is really an example illustrating one reason why global variables are considered things to avoid almost all of the time. Had main() required "rule" as a parameter then it would not have been possible to call it without at least providing a rule. The same applies with most other functions: if they all require their external state via parameters then you can't miss things out. (You can, of course, always pass incorrect values, but that is usually easier to debug.) Avoid globals; they are usually a source of bugs. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
What's the difference between running a script under command box and interpreter?
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 "", line 1, in 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. I also tried "py -m pdb test.py" and step through it, no problem too. Why? --Jach -- https://mail.python.org/mailman/listinfo/python-list