I. A Simple Class
Begin by saving the following very simple class to a text file named seqclass.py:
class SeqClass:
def
store_data(self, sequence):
self.data =
sequence
def
display(self):
print self.data
Exercise 1:
(1) Open the Python interpreter and use the execfile() command to execute the file. (You can also use the import command, but import is better after you have completely finished the class and will make no more changes.)
(2) In the interpreter, make an instance of the class called seq and call the display method. What does it print?
(3) Now call the store_data method of seq with one argument ('GAATTACAT') and then call the display method. What does it print now?
In SeqClass, the data attribute (variable) did not exist until you called the store_data method. The store data method then created an attibute named data and assigned it a value (the argument).
II. Initializing data during instantiation
Now let's change the class to that the data attribute begins with a value so that it does not throw an error if you call the display method before the store_data method.
class SeqClass:
def
__init__(self):
self.data = 'No
Data'
def
store_data(self, sequence):
self.data =
sequence
def
display(self):
print self.data
This example uses the __init__ built-in method which is called every time you create an instance of the object. For example, if you make an new instance of SeqClass:
>>>x = SeqClass() #x is an instance of SeqClass, x.data set to 'No Data'
>>>test = x.data
>>>print test
No Data
Repeat parts 1 to 3 with the new version of SeqClass (remember to save your changes to the file first).
Exercise 2:
(1) Alter the SeqClass so that the data attribute is initialized to be an empty list.
(2) Change the store_data method so that it appends to the list instead of just assigning to a variable name.
(3) Change the display method so that it prints out every element of the data list one at a time. [Hint: Use a for loop within the display method]
(4) Repeat steps 1-3 of Exercise 1. Then call the store_data method two more times with the following arguments before calling the display method:
'GACCAT'
'Spam I am'
Did the display method print out all elements of data one at a time? If not, try try again!
(5) Make a new instance of the class called seq2 and call seq2.display(). What does it print? Do you understand why?
III. Calling methods within methods
Just like you can call functions within functions, you can also call class methods within other class methods. The syntax is a little different, but not too bad (really!). Basically, all you have to do is all the "self" before the method call and it will work.
Example:
class SeqClass:
<...>
def
store_data(self, sequence):
<...>
self.display() #calls display method
<...>
Save and execute this new version of SeqClass. Then make an instance of the class and call the store_data method with the same three arguments as above. Does the method print out the elements of the list each time?
Exercise 3:
(1) Add a new method (check_data) to SeqClass that checks to see if the data list is empty - the new method will return a 0 if empty, and a 1 if not empty.
[Hint: use the len() function]
(2) Call the check_data method within display. If the check_data method returns 'true' (i.e., 1) print all the elements of the list. Else just print the statement "No data in list"
(3) Create a new instance of the new SeqClass and call display. Call the store_data method with 'GAACATT' then call the display method again. Did you get what you expected?
Final Exercise:
In preparation for the next class homework assignment, make a class called Protein that has the following data and methods:
(1) Each instance should be initialized with a dictionary containing a genetic code table (start with the universal code).
(2) Create a method called return_aa which returns the proper amino acid given a codon string as an argument.
(3) Pre-condition check: within return_aa check that the condon string argument is exactly 3 letter long and contains only four possible letters: U, G, C, or A.
[Hint: Use the len() command, if/else statement. Use a for loop to check if each letter in the string is A,G,U, or T. You can make it a little more simple by checking each letter with the re.match([AGCU]) command.]