If you import the contents of that file into the python interpreter, [...]
Op 6/03/2024 om 13:55 schreef Jacob Kruger via Python-list:(As an aside, I made a type; "What exactly _do_ you mean" is what I
If you import the contents of that file into the python interpreter,
[...]
What exactly to you mean by "import the contents of that file into the python interpreter"? Other people have put your code in a script,
executed it, and saw it working as expected. I pasted in IPython, and likewise saw it working as expected, and the same with IDLE. It seems
to me you must be doing something different from us; maybe the way you execute that code might be the key to this whole confusion.
1970-01-01 00:00:00+00:00from scoping2 import *
print(dt_expiry)
1808577867152print(id(dt_expiry))
date value 2024-03-06 18:39do_it()
1970-01-01 00:00:00+00:00print(dt_expiry)
1808577867152print(id(dt_expiry))
---end session---
On 3/6/24 05:55, Jacob Kruger via Python-list wrote:
Ok, simpler version - all the code in a simpler test file, and
working with two separate variables to explain exactly what am
talking about:
If you import the contents of that file into the python interpreter,
dt_expiry will start off as "1970-01-01 00:00", and, if you execute
do_it function, it will print out the new value assigned to the
dt_expiry variable inside that function, but if you then again check
the value of the dt_expiry variable afterwards, it's reverted to the
1970... value?
If I take out the line that removes values from l_test #
l_test.clear() # before appending new value to it, then it will also
not retain it's new/additional child items after the function exits,
and will just revert back to [1, 2, 3] each and every time.
In other words, with some of the variable/object types, if you use a
function that manipulates the contents of a variable, before then
re-assigning it a new value, it seems like it might then actually
update/manipulate the global variable, but, either just calling
purely content retrieval functions against said objects, or assigning
them new values from scratch seems to then ignore the global scope
specified in the first line inside the function?
Hope this makes more sense
No, it doesn't. Your code is working as one would expect. For example, adding prints for the l_test variable, and removing the .clear() which
you claim makes it not work, shows me:
before: l_test=[1, 2, 3], id(l_test)=140153285385856
leaving do_it: l_test=[1, 2, 3, 1, 2, 3, 99], id(l_test)=140153285385856 after: l_test=[1, 2, 3, 1, 2, 3, 99], id(l_test)=140153285385856
It's the same list object, as you can see by the id values. And the
list is updating as expected.
And... you don't need the global statement for l_test. As it's
mutable, you can mutate it in the function; the global only acts on assignment. Using "global" for that may make your intent more clear to readers though, although static checkers will grumble at you.
You must be doing something additional that you're not telling us about.
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.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 915 |
Nodes: | 10 (1 / 9) |
Uptime: | 28:14:22 |
Calls: | 12,169 |
Calls today: | 1 |
Files: | 186,521 |
Messages: | 2,234,145 |