首页 > 解决方案 > 在 Python 中定义额外变量会导致错误

问题描述

我有这个代码片段:(我认为代码的其他部分并不重要。)

mol = editable_mol.GetMol()
neighbors = mol.GetAtomWithIdx(lastAtomIndex).GetNeighbors()
for nb_atom in neighbors:
    atom_type, h_aro, h_alp, h_beta, h_gam = common.getAtomTypeAndHmnr(nb_atom)
    if atom_type == 9:
        common.setAtomTypeAndHmnr(nb_atom, 9, 0, 1, 0, 0)
        Chem.EditableMol.ReplaceAtom(editable_mol, nb_atom.GetIdx(), nb_atom)

它运行完美。但是,如果我使用这样的变量editable_mol.GetMol()代替:mol

neighbors = editable_mol.GetMol().GetAtomWithIdx(lastAtomIndex).GetNeighbors()
for nb_atom in neighbors:
    atom_type, h_aro, h_alp, h_beta, h_gam = common.getAtomTypeAndHmnr(nb_atom)
    if atom_type == 9:
        common.setAtomTypeAndHmnr(nb_atom, 9, 0, 1, 0, 0)
        Chem.EditableMol.ReplaceAtom(editable_mol, nb_atom.GetIdx(), nb_atom)

程序给出错误。这意味着我必须使用 mol 变量。但我不明白为什么我必须使用它。在 python 中定义额外变量是否会改变?我认为这两个代码是相同的。

程序给出了这个错误,但我认为这个错误与内容无关:

atom_type, h_aro, h_alp, h_beta, h_gam = common.getAtomTypeAndHmnr(nb_atom) 文件“C:\Users\phitech_halil\Documents\Tupras Projects\MolGenPython\phitech\common.py”,第 302 行,在 getAtomTypeAndHmnr return int(atom.GetProp (KEY_ATOM_TYPE)), int(atom.GetProp(KEY_H_AROMATIC)), int(atom.GetProp(KEY_H_ALPHA)), \ KeyError: 'atom_type'

唯一不同的是定义mol变量,但程序给出了错误。我认为这个问题与editable_mol对象或GetMol()功能无关。有什么想法吗?

标签: python

解决方案


推荐阅读