[英]Debugging Python: Why don't my variables update?
I'm using PyCharm 2019.2 Professional, Win 10 x64, Python 3.7, and IPython 7.11.1.我使用的是 PyCharm 2019.2 Professional、Win 10 x64、Python 3.7 和 IPython 7.11.1。
When running a script in debug mode and hitting a breakpoint, I can execute statements in the IPython prompt.在调试模式下运行脚本并遇到断点时,我可以在 IPython 提示符下执行语句。 However, I (sometimes?) cannot change the variables values.但是,我(有时?)无法更改变量值。
For example, I have a dataframe and check on some condition to get a bool-Series, which I then sum up to check the number of thruths in the dataframe, and if there are none, append another dataframe:例如,我有一个数据帧并检查某些条件以获取 bool-Series,然后我对其进行总结以检查数据帧中的 thruths 数量,如果没有,则附加另一个数据帧:
(note that this is a simplified example, I'm not looking to improve this specific snipped) (请注意,这是一个简化的示例,我不打算改进此特定剪辑)
a_lim = 0.01
my_mask = df_old['A'] < a_lim
if sum(my_mask) == 0:
df_new = generate_new_df()
df_old = df_old.append(df_new, ignore_index=True) \
.sort_values(by=['A'], ascending=True) \
.reset_index(drop=True)
pass
Lets assume df_new contains one row of data that evaluates as True.让我们假设 df_new 包含一行评估为 True 的数据。 If I set a breakpoint the if-statement, sum(my_mask)
would be 0. If I set my breakpoint at pass
, I can check df_old
and see the added rows from df_new.如果我在 if 语句中设置断点, sum(my_mask)
将为 0。如果我在pass
设置断点,我可以检查df_old
并查看df_old
中添加的行。 At that point, sum(my_mask)
is still 0. That is fine.那时, sum(my_mask)
仍然是 0。那很好。
My Problem:我的问题:
Stopped at pass
, I evaluate my_mask = df_old['A'] < a_lim
.在pass
停止,我评估my_mask = df_old['A'] < a_lim
。 Then, I check sum(my_mask)
and it still returns 0 .然后,我检查sum(my_mask)
并且它仍然返回 0 。 However, if I evaluate sum(df_old['A'] < a_lim)
, I will get 1 (the expected result).但是,如果我评估sum(df_old['A'] < a_lim)
,我将得到 1 (预期结果)。
What is happening behind the scenes, that Python/IPython seems to selectively update variables, and not others?幕后发生了什么,Python/IPython 似乎有选择地更新变量,而不是其他变量?
Thanks!谢谢!
I have encountered this bug as well.我也遇到过这个bug。 I have always assumed it is a PyCharm bug not Python.我一直认为这是 PyCharm 错误而不是 Python。 Might be worth raising a bug with JetBrains, think you can do that here:可能值得用 JetBrains 提出一个错误,认为你可以在这里做到这一点:
https://youtrack.jetbrains.com/issues/PY https://youtrack.jetbrains.com/issues/PY
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.