#1 Classes and Objects

Classes and Objects are an essential part in the programming paradigm called “object-oriented programming“.  Compared to “classical” procedural code – just like C – it is a form of code abstraction. It is mainly powerful when you’re working with a lot of instances (instance is at the same time a technical term) of the same kind. And luckily, we have this feature in Python as well.

Let’s go to an example. When we think about classes, we think exactly of what it means. Classes of things, animals, whatever. In other words, imagine you have a car class, and you will work with objects belonging to that class, here: cars such as a Mercedes, Opel, Audi, whatever. Let’s look closer at this example:

class Car:
    def __init__ (self):
        pass

Now, we created a class called Car , and added a function called  __init__ . Important is the first argument self is passed. This tells Python, that the function belongs to the class. That is, however, basically not more than a convention.

Every time we create (better: construct) an object, i.e. an instance of the class Car, it calls automatically the init function, a.k.a. constructor:

class Car:
    def __init__ (self):
        print("initialized")

Opel = Car() 
Benz = Car()
# Out:
# initialized
# initialized

We created two objects of the class Car, we called them Opel and Benz. We can create for example further functions, pass arguments or use class-internal variables:

class Car:    
    def __init__ (self, brand):
        print("initialized")
        self.brand = brand
        self.buildingYear = 2008
        
    def age (self, currentYear):
        print("This car is", currentYear-self.buildingYear,"years old!")

myCar = Car('Mercedes')
myCar.age(2016) # will print "This car is 8 years old!"

Your homework is creating a class that is capable of

  • Initialize a car with the following arguments: brand, building year, mileage and color.
  • Set the default color to ‘colorful’
  • Create a function that returns and prints the car’s color. It should also print the car brand.
  • Create a function that calculates the mileage per year/month/week and returns that.
    Pass an argument to the function of the current year and the time frame. Use “year” as default time frame.
  • Calculate the car’s worth using the Python module math and an exponential decay. Try to find a lambda value, that results in a 25% loss per year.

Here are three cars, that should be tested with various settings:

  • A 10 year old Mercedes-Benz that has 213,235 km mileage and its color is silver
  • A T1 Bully, BY 1982 with exactly 999,999 km. Color unknown.
  • A brownish Mini, 2 years old, 11,111 km mileage.

Have fun!

A more scientific example

Just imagine you want to get some metadata for your gene of interest (GOI). You can create for example a class, that reads in a fasta-file, parses it and returns GC content and potential open reading frames (ORFs).

  • create a class that requires a filename (with path) as argument.
  • read in the file (here’s beta actin, try it for your GOI), save the name (e.g. ACTB) and the sequence as separate variables or as a dictionary.
  • create a function that calculates the GC content (hint: use “MYSEQUENCE”.count()…)
  • create a function that returns the potential ORFs. (same hint as above…)
  • if you’re ambitious, you can use the universal code to translate the sequence to amino acids (an example how you could do it). Maybe you can select the ORF?

Stay tuned for a solution that addresses all the aforementioned features (the day before the next session).

Please find here a possible solution to the homework. If you have any questions, please let me know. There’s some advanced stuff in the 2nd task, but I am sure you could manage that.