我正在构建一个具有类变量字典的模块:
class CodonUsageTable:
CODON_DICT={'TTT': 0, 'TTC': 0, 'TTA': 0, 'TTG': 0, 'CTT': 0,
'CTC': 0, 'CTA': 0, 'CTG': 0, 'ATT': 0, 'ATC': 0,
'ATA': 0, 'ATG': 0, 'GTT': 0, 'GTC': 0, 'GTA': 0,
'GTG': 0, 'TAT': 0, 'TAC': 0, 'TAA': 0, 'TAG': 0,
'CAT': 0, 'CAC': 0, 'CAA': 0, 'CAG': 0, 'AAT': 0,
'AAC': 0, 'AAA': 0, 'AAG': 0, 'GAT': 0, 'GAC': 0,
'GAA': 0, 'GAG': 0, 'TCT': 0, 'TCC': 0, 'TCA': 0,
'TCG': 0, 'CCT': 0, 'CCC': 0, 'CCA': 0, 'CCG': 0,
'ACT': 0, 'ACC': 0, 'ACA': 0, 'ACG': 0, 'GCT': 0,
'GCC': 0, 'GCA': 0, 'GCG': 0, 'TGT': 0, 'TGC': 0,
'TGA': 0, 'TGG': 0, 'CGT': 0, 'CGC': 0, 'CGA': 0,
'CGG': 0, 'AGT': 0, 'AGC': 0, 'AGA': 0, 'AGG': 0,
#Other code
def __init__(self,seqobj):
'''Creates codon table for a given Bio.seq object.i
The only argument is Bio.Seq object with DNA
Currently assumes seq to be DNA, RNA support to be added later'''
dnaseq=str(seqobj)
self.usage_table=CodonUsageTable.CODON_DICT.deepcopy()#instance of table
最后一行必须复制一个类字典以在其中存储实例数据,但它会抛出
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "./codon_usage.py", line 47, in __init__
self.usage_table=CodonUsageTable.CODON_DICT.deepcopy()#instance of codon usage table
NameError: global name 'CODON_DICT' is not defined
从self.CODON_DICT
调用CODON_DICT
,codon_usage.CodonUsageTable.CODON_DICT
或__init__
时也是如此。字典定义如下:
>>>import codon_usage
>>> codon_usage.CodonUsageTable.CODON_DICT
{'GCT': 0, 'GGA': 0, 'TTA': 0, 'GAT': 0, 'TTC': 0, 'TTG': 0, 'AGT': 0, 'GCG': 0, 'AGG': 0, 'GCC': 0, 'CGA': 0, 'GCA': 0, 'GGC': 0, 'GAG': 0, 'GAA': 0, 'TTT': 0, 'GAC': 0, 'TAT': 0, 'CGC': 0, 'TGT': 0, 'TCA': 0, 'GGG': 0, 'TCC': 0, 'ACG': 0, 'TCG': 0, 'TAG': 0, 'TAC': 0, 'TAA': 0, 'ACA': 0, 'TGG': 0, 'TCT': 0, 'TGA': 0, 'TGC': 0, 'CTG': 0, 'CTC': 0, 'CTA': 0, 'ATG': 0, 'ATA': 0, 'ATC': 0, 'AGA': 0, 'CTT': 0, 'ATT': 0, 'GGT': 0, 'AGC': 0, 'ACT': 0, 'CGT': 0, 'GTT': 0, 'CCT': 0, 'AAG': 0, 'CGG': 0, 'AAC': 0, 'CAT': 0, 'AAA': 0, 'CCC': 0, 'GTC': 0, 'CCA': 0, 'GTA': 0, 'CCG': 0, 'GTG': 0, 'ACC': 0, 'CAA': 0, 'CAC': 0, 'AAT': 0, 'CAG': 0} 'GGT': 0, 'GGC': 0, 'GGA': 0, 'GGG': 0}
答案 0 :(得分:1)
症状暗示这个故事是这样的:
CODON_DICT
无法像这样被访问并修复了它; 这是因为Python仍在使用导入过程中加载的旧版本模块。虽然它显示了新文件中的行,但由于它在内存中的所有内容都是带有元数据的字节码,并且在发生错误时必须引用磁盘。如果要在不重新启动shell的情况下选择最新更改,请运行:
>>> reload(codon_usage)
再试一次。
(旁注:dict
没有方法deepcopy
,该功能来自模块copy
。dict.copy
就足够了。