Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class constant dictionary in Python

I'm building a module that has a class variable dictionary:

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

The last line must make a copy of class dictionary to store instance data in it, but it throws

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

So does self.CODON_DICT, CODON_DICT or codon_usage.CodonUsageTable.CODON_DICT, when called from __init__. Dictionary is defined:

>>>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}
like image 278
Synedraacus Avatar asked Apr 23 '14 03:04

Synedraacus


People also ask

What is a class constant Python?

In programming, the term constant refers to names representing values that don't change during a program's execution. Constants are a fundamental concept in programming, and Python developers use them in many cases.

Can we create constant Dictionary in Python?

No there is not. You cannot declare a variable or value as constant in Python. Just don't change it.

How do you store constants in Python?

In Python, constants are usually declared and assigned in a module (a new file containing variables, functions, etc which is imported to the main file). In the above example, we created the constant.py module file. Then, we assigned the constant value to PI and GRAVITY .

Can a constant be a list?

A constant list is a series of constant values that are enclosed in parentheses, separated by commas or blanks, that can be used in simple array assignments and in IN expressions. A constant list can also contain nested constant lists. An example of a constant list is (2, '33', –5, (1, '3')) .


1 Answers

The symptoms imply that the story went like this:

  1. you wrote the file and saved it;
  2. you ran the Python shell;
  3. you found that CODON_DICT can't be accessed just like that and fixed that;
  4. you tried that call again within the same Python shell and got that error.

That happens because Python is still using the old version of the module, which is loaded during the import. Although it shows the line from the new file, since all it has in the memory is the bytecode with metadata and has to refer to the disk when error happens. If you want to pick your latest changes without restarting the shell, run:

>>> reload(codon_usage)

and try again.

(A sidenote: dict has no method deepcopy, that function comes from the module copy. dict.copy is enough here, though).

like image 185
bereal Avatar answered Oct 18 '22 13:10

bereal