0

Code speaks better:

import numpy as np
a = np.ones(shape=(4, 2))
def func():
    for i in a:
        print(i)

Run:

In[3]: func()
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]
In[4]: a = np.zeros(shape=(4, 2))
In[5]: func()
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]

Notice that I changed (a). But, when I run the function again, no changes!! Details: latest version of Pycharm. Configs > Execution: Run with Python console.

LiuXiMin
  • 1,225
  • 8
  • 17
Alex Deft
  • 2,531
  • 1
  • 19
  • 34
  • Originally, this started as a massive neural net with modular design. Following 3 days of debugging, I reduced it to that. – Alex Deft Jun 16 '19 at 01:08
  • Possible duplicate : https://stackoverflow.com/questions/51239527/how-to-reload-updated-function-in-python-console-in-pycharm – Xavi Martínez Jun 16 '19 at 01:51
  • @XaviMartínez No, I'm not updating the function. But yes, solve my problem, I just need to define the same function again and it works! But why is that given that the function did not change. It was (a) who changed. – Alex Deft Jun 16 '19 at 02:12

1 Answers1

1

I don't use Pycharm. But I think I know why.

When you Run with Python console, it should have from your-source-file import * in background.

When you rebind a to new object in console, the func will still use the a in your-source-file, not the a in console.

You can have a try by explicitly from your-source-file import * and take the rest of actions to verify it. I have checked it on my computer by myself.

If you want understand why, you can read 4. Execution model: resolution-of-names — Python 3.7.3 documentation, and make sure you understand this:

When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s environment.

My try in ipython:

In [2]: from test import *

In [3]: func()
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]

In [4]: a = np.zeros(shape=(4, 2))

In [5]: func()
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]

In [6]: def func():
   ...:     for i in a:
   ...:         print(i)
   ...:

In [7]: func()
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]

and

In [1]: from auto_audit_backend.test_np import *

In [2]: func()
[1. 1.]
[1. 1.]
[1. 1.]
[1. 1.]

In [3]: a[0][0] = 666

In [4]: func()
[666.   1.]
[1. 1.]
[1. 1.]
[1. 1.]

In [5]: a = np.zeros(shape=(4, 2))

In [6]: func()
[666.   1.]
[1. 1.]
[1. 1.]
[1. 1.]

with your code in test.py file.

LiuXiMin
  • 1,225
  • 8
  • 17
  • Woah, you are a genius, you regenerated the problem in a different platform. So, you're saying no bugs with Pycharm? – Alex Deft Jun 16 '19 at 02:52
  • How do I prevent this from happening? This is causing me a headache while I'm experimenting with neural nets. – Alex Deft Jun 16 '19 at 02:55
  • @AlexDeft Yes, I think no bugs with Pycharm, you can just try in Pycharm console. It is the rule of Python. – LiuXiMin Jun 16 '19 at 02:56
  • 2
    @AlexDeft pass your variable to func, not use global variable, which is a bad habit and would be invalid when you import it from somewhere else. – LiuXiMin Jun 16 '19 at 02:58