How does Python find the value referenced by a variable? Python stores bindings in a Namespace.
![]() | Namespace |
|
A namespace is a mapping of variable names to their values. | |
You can also think about a namespace as a sort of dictionary containing all defined variable names and the corresponding reference to their values.
![]() | Reference |
|
A reference is a sort of pointer to a location in memory. | |
Therefore you do not have to know where exactly your value can be found in memory, Python handles this for you via variables.
Figure 2.1 shows a representation of some namespace. Values which have not been referenced by a variable, are not accessible to you, because you can not access the memory space directly. So if a result of a calculation is returned, you can use it directly and forget about it after that. Or you can create a variable holding this value and then access this value via the variable as often as you want.
>>> from string import *
>>> cds = """atgagtgaacgtctgagcattaccccgctggggccgtatatcggcgcacaaa
tttcgggtgccgacctgacgcgcccgttaagcgataatcagtttgaacagctttaccatgcggtg
ctgcgccatcaggtggtgtttctacgcgatcaagctattacgccgcagcagcaacgcgcgctggc
ccagcgttttggcgaattgcatattcaccctgtttacccgcatgccgaaggggttgacgagatca
tcgtgctggatacccataacgataatccgccagataacgacaactggcataccgatgtgacattt
attgaaacgccacccgcaggggcgattctggcagctaaagagttaccttcgaccggcggtgatac
gctctggaccagcggtattgcggcctatgaggcgctctctgttcccttccgccagctgctgagtg
ggctgcgtgcggagcatgatttccgtaaatcgttcccggaatacaaataccgcaaaaccgaggag
gaacatcaacgctggcgcgaggcggtcgcgaaaaacccgccgttgctacatccggtggtgcgaac
gcatccggtgagcggtaaacaggcgctgtttgtgaatgaaggctttactacgcgaattgttgatg
tgagcgagaaagagagcgaagccttgttaagttttttgtttgcccatatcaccaaaccggagttt
caggtgcgctggcgctggcaaccaaatgatattgcgatttgggataaccgcgtgacccagcacta
tgccaatgccgattacctgccacagcgacggataatgcatcgggcgacgatccttggggataaac
cgttttatcgggcggggtaa""".replace("\n","")
>>> float(count(cds, 'G') + count(cds, 'C'))/ len(cds)
0.54460093896713613
Here the result of the gc-calculation is lost.
>>> gc = float(count(cds, 'G') + count(cds, 'C'))/ len(cds)
>>> gc
0.54460093896713613
In this example you can remember the result of the gc
calculation, because it is stored in the variable
gc.