Do functions from an imported module stay in a defined state? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Do functions from an imported module stay in a defined state?

First an example to explain what I am talking about: def f(l=[]): l.append(True) Setting up a default list like this leads to it being defined only once together with the function definition - so it will keep its state and accumulate one more True with every call. If this function was an embedded function, the state would be forgotten after the outer function is returned from, and the list would start empty with the next outer function call. So now my question: If the function above was part of a module m and I imported it, would m stay defined? In other words, does the imported module belong to the global sphere (even if separated by '.') and m.f's list would keep its state if I called it by m.f() several times?

23rd Dec 2018, 11:32 AM
HonFu
HonFu - avatar
1 Answer
+ 1
Now, back at my own pc, I was able to check it myself. ;-) As it should be, m.f stays defined and acts like it was in globals.
26th Dec 2018, 6:09 PM
HonFu
HonFu - avatar