1

Possible Duplicate:
Help with Python UnboundLocalError: local variable referenced before assignment

In python3, I have two classes, one for DNA and one for RNA. I would like the DNA class to have a method that takes the DNA sequence (an instance variable of DNA, self.sequence), changes it into an RNA sequence (easily done with a for loop), and then creates an RNA object with the new sequence as an instance variable.

At the same time, I would like the RNA class to have a method that does the opposite (That is, it takes the RNA sequence, produces a corresponding DNA sequence, and then creates a DNA object that uses the sequence as an instance variable.)

My method for making RNA from DNA is as follows:

def transcribe(self):
    RNAseq=''
    for base in self.sequence:
        if base=='A' or base=='C' or base=='G':
            RNAseq=RNAseq+base
        if base=='T':
            RNAseq=RNAseq+'U'
    RNA=RNA(RNAseq,self.name+'RNA')
    return RNA

This code gives me the error: UnboundLocalError: local variable 'RNA' referenced before assignment

Can what I want to do be done?

1
  • 1
    Is RNA the name of your RNA class? Commented Dec 7, 2012 at 7:00

1 Answer 1

5

Call your variable something other than RNA, e.g. rna:

rna = RNA(RNAseq, self.name + 'RNA')
return rna

Otherwise you're trying to use the same name for two different things, the class and the variable, and the latter shadows the former.

Sign up to request clarification or add additional context in comments.

2 Comments

That worked great, thanks! I feel a little silly now, but lesson learned!
@user1884604: No need to feel silly. That error message isn't terribly clear.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.