Sujet : Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference
De : jacob.kruger.work (at) *nospam* gmail.com (Jacob Kruger)
Groupes : comp.lang.pythonDate : 07. Mar 2024, 10:23:27
Autres entêtes
Message-ID : <mailman.58.1709799818.3452.python-list@python.org>
References : 1 2 3 4 5 6 7 8
User-Agent : Mozilla Thunderbird
Thanks again, all.
I think the python -i scoping2.py would have given me a good beginning as well - will archive that one for use.
And, to maybe explain how I work - not an excuse at all - but, I am actually 100% blind, so a lot of the IDE's, or their common means/methods of interaction don't suit me all the time, which is why I generally work via programmer's text editor interfaces, or treat something like VS code as such, but then still prefer to run my code via command line, using pdb to then play around with forms of debugging, etc.
And, yes, also generally prefer to work via classes, modules, etc. at runtime, but this was more or less mostly testing, which then caused confusion/interference on my side...LOL!
Jacob Kruger
+2782 413 4791
"Resistance is futile!...Acceptance is versatile..."
On 2024/03/07 03:55, Grant Edwards via Python-list wrote:
On 2024-03-07, dn via Python-list <python-list@python.org> wrote:
>
The idea of importing a module into the REPL and then (repeatedly)
manually entering the code to set-up and execute is unusual (surely type
such into a script (once), and run that (repeatedly). As you say, most
of us would be working from an IDE and hitting 'Run'. Am wondering why
you weren't - but it's not important.
Unless the code is intended to be used as a module, 'import'ing it into
the REPL doesn't make sense.
>
A simple example:
>
---------------------------testit.py------------------------------
x = 'x'
y = 'y'
def foo():
global y
print("hi")
x = 'X'
y = 'Y'
print(x)
print(y)
------------------------------------------------------------------
>
The usual method to play with that interactively is
>
$ python -i testit.py
>>> x
'x'
>>> y
'y'
>>> foo()
hi
X
Y
>>> x
'x'
>>> y
'Y'
>>>
>
As we've seen, doing a 'from testit.py import *' doesn't let you test
what the OP was trying to test. Doing 'import testit.py' gets you
closer, but it's a hassle to test code that way. The right thing to do
is 'python -i <filename>' (or the equivalent button/option in an IDE).
>
https://docs.python.org/3/tutorial/interpreter.html
>
If you intended to use testit.py as a module, and wanted to experiment
with its behavior as a module, then go ahead and import it. But, don't
do 'from testit.py import *' until
>
1. you know how that differs from 'import testit.py'
>
and
>
2. you want to use that difference
>
>