为什么使用 python 调试器只能分配某些变量?

发布于 2025-01-15 09:13:43 字数 875 浏览 5 评论 0原文

在调试我的代码时,我偶然发现了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2025-01-22 09:13:43

a = ...b = ... 不被识别为 Python 语句,因为 aargs 的缩写 命令,bbreak 命令的缩写。

  • args 显然忽略了任何参数。
  • break 拒绝将 = pos_frame[a].values 作为函数或文件名,就像以下错误消息状态一样。
  • pos_frame[a] 明确是一条 Python(表达式)语句,但由于您之前尝试定义 a 失败,因此该语句的执行失败,并出现 NameError< /代码> 显示。

当 Python 语句可以被识别为调试器命令时,使用 ! 命令显式执行该语句。

(Pdb) ! a = pos_fram.columns[-1]
(Pdb) ! b = pos_frame[a].values

(一般情况下,请使用 !,除非您知道没有必要。x 不是调试器命令,因此 x 是否是没有任何歧义。 code>x = 4 是要执行的调试器命令或 Python 语句。)

a = ... and b = ... are not recognized as Python statements, as a is an abbreviation of the args command and b is an abbreviation of the break 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 define a earlier failed, the execution of this statement fails with the NameError shown.

Use the ! command to explicitly execute a Python statement when the statement can be recognized as a debugger command.

(Pdb) ! a = pos_fram.columns[-1]
(Pdb) ! b = pos_frame[a].values

(In general, use ! unless you know it isn't necessary. x is not a debugger command, so there is no ambiguity whether x = 4 is a debugger command or a Python statement to execute.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文