为什么使用 python 调试器只能分配某些变量?
在调试我的代码时,我偶然发现了 pdb 的这种行为(请参见下面的代码),您只能分配某些类型的变量。为了澄清起见,我使用 pandas DataFrames 并希望将其中一列的名称分配给一个新变量以供快速参考,并且这样做时没有给出错误。但是当我想使用我的新变量时,根据我如何使用该变量,我得到了不同的错误。这种行为从何而来,其意图何在?
(Pdb) pos_frame.columns[-1]
'Position 13'
(Pdb) a = pos_frame.columns[-1]
(Pdb) b = pos_frame[a].values
*** The specified object '= pos_frame[a].values' is not a function or was not found along sys.path.
(Pdb) pos_frame[a]
*** NameError: name 'a' is not defined
(Pdb) a
(Pdb) x = 4
(Pdb) x
4
*编辑 一些娱乐代码
import numpy as np
import pandas as pd
import pdb
# creating a DataFrame
col_list = [
np.r_[0:50],
np.r_[50:100]
]
data_frame = pd.DataFrame(col_list).transpose()
data_frame.columns = [f"Position {i}" for i in range(1, len(col_list)+1)]
pdb.set_trace()
while debugging my code, I stumbled upon this behavior of pdb (see code below), where you can assign only certain types of variables. For clarification, I was using pandas DataFrames and wanted to assign the name of one of the columns to a new variable for quick reference, and no error was given doing just that. But when I wanted to use my new variable, I got different errors depending on how I used that variable. Where does this behavior come from and is it intended?
(Pdb) pos_frame.columns[-1]
'Position 13'
(Pdb) a = pos_frame.columns[-1]
(Pdb) b = pos_frame[a].values
*** The specified object '= pos_frame[a].values' is not a function or was not found along sys.path.
(Pdb) pos_frame[a]
*** NameError: name 'a' is not defined
(Pdb) a
(Pdb) x = 4
(Pdb) x
4
*edit
some code for recreation
import numpy as np
import pandas as pd
import pdb
# creating a DataFrame
col_list = [
np.r_[0:50],
np.r_[50:100]
]
data_frame = pd.DataFrame(col_list).transpose()
data_frame.columns = [f"Position {i}" for i in range(1, len(col_list)+1)]
pdb.set_trace()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
a = ...
和b = ...
不被识别为 Python 语句,因为a
是args 的缩写
命令,b
是break
命令的缩写。break
拒绝将= pos_frame[a].values
作为函数或文件名,就像以下错误消息状态一样。pos_frame[a]
明确是一条 Python(表达式)语句,但由于您之前尝试定义a
失败,因此该语句的执行失败,并出现NameError< /代码> 显示。
当 Python 语句可以被识别为调试器命令时,使用
!
命令显式执行该语句。(一般情况下,请使用
!
,除非您知道没有必要。x
不是调试器命令,因此x
是否是没有任何歧义。 code>x = 4
是要执行的调试器命令或 Python 语句。)a = ...
andb = ...
are not recognized as Python statements, asa
is an abbreviation of theargs
command andb
is an abbreviation of thebreak
command.args
apparently ignores any arguments.break
is rejecting= pos_frame[a].values
as a function or file name, just like the following error message states.pos_frame[a]
is unambiguously a Python (expression) statement, but as your attempt to definea
earlier failed, the execution of this statement fails with theNameError
shown.Use the
!
command to explicitly execute a Python statement when the statement can be recognized as a debugger command.(In general, use
!
unless you know it isn't necessary.x
is not a debugger command, so there is no ambiguity whetherx = 4
is a debugger command or a Python statement to execute.)