0

I'm taking an online Python course and in one of the lectures, we wrote a program that reads dna sequences from a file and puts them into a dictionary. The file being read has the following form

>header1
dna sequence 1
>header2
dna sequence 2
>header3
dna sequence 3
...

An example file would be

>seq1
aaacgtgtgccccgatagttgtgtcagt
>seq2
acccgtgcacacagtgccaaggggatat
atagatatc
>seq3
agctcgatcgatcgattttagcgagagg
gagagacttcgatcgatcgagtcgatcg
a

Here's the program:

try:
    f = open("fasta.txt")
except IOError:
    print("Coulnd't open file")

seqs = {}

for line in f:
    line = line.rstrip()
    if (line[0] == ">"):
        words = line.split()
        name = words[0][1:]
        seqs[name] = ''
    else:
        seqs[name] = seqs[name] + line

f.close()

print(seqs['seq5'])

My question is, why does this program work? From what I know about programming languages, variables have a scope in the block in which they are defined. In the program, the variable name, is defined in the "if" part of the program, but then it's referenced in the "else" part of the program. But the only way that a program is going to enter the "else" part of the program is if it doesn't enter the "if" part, so it won't encounter the variable name. So in my mind, this program shouldn't be working. But it does for some reason.

So I wanted to ask, why it's working. How do variable scopes work in Python?

4
  • 1
    it works on the assumption the if is True the first time it is seen, and then name is defined. Afterwards name is only updated when a new line has the signal >. If this assumption does not hold on your input file you will receive a KeyError
    – Attack68
    Mar 9, 2019 at 18:09
  • 1
    There is always a line starting with '>' first, so name will get its value then, and will be used again in the else part when you get to the next line. Note that blocks like if, for and so on don't have their own scope: the closest scope here is the function. Mar 9, 2019 at 18:09
  • That's a lot of irrelevant information just to ask the question "why can a variable declared in an if be accessed in the else". Could you maybe reduce the question to only the necessary bits? As you can see, at least 3 people have already misunderstood your question.
    – Aran-Fey
    Mar 9, 2019 at 18:11
  • By the by, your program should exit in the except clause, or simply not have a try around the open. There is no way the program can complete successfully if it can't read the input data, yet that is precisely what this code attempts. The ultimate lesson is to not have an except handler unless you really can handle the raised exception somehow. (Using it to produce a more meaningful error message than a backtrace is fine, but then really do make sure to terminate after printing the message. Also, diagnostics should go to standard error.)
    – tripleee
    Mar 9, 2019 at 21:27

1 Answer 1

0

From reading your data format, it appear that the if statement would be read first, thus initializing name with words[0][1:]. All following statements would be valid because name already exists.

Not the answer you're looking for? Browse other questions tagged or ask your own question.