Understanding Function, Methods, and Class Methods

- 5 mins
%load_ext autoreload
%autoreload 2

Functions vs. Methods

Sometimes people do not realize there is a difference between functions and methods. It is important to be able to know the difference and when and how to call them. In your coding journey you will likely encounter functions, methods, and class methods. Understanding the difference will allow you to utilize these tools to their greatest effect.

Functions

Lets start with functions since everyone that has written any code has used one wether or not they have recognized it.

What is a function?

# example of importing pandas and calling a function from it
import pandas as pd

pd.read_csv("../data/dc-comics.csv")
page_id name urlslug ID ALIGN EYE HAIR SEX ALIVE APPEARANCES FIRST APPEARANCE YEAR
0 1422 Batman (Bruce Wayne) \/wiki\/Batman_(Bruce_Wayne) Secret Identity Good Characters Blue Eyes Black Hair Male Characters Living Characters 3093.0 1939, May 1939.0
1 23387 Superman (Clark Kent) \/wiki\/Superman_(Clark_Kent) Secret Identity Good Characters Blue Eyes Black Hair Male Characters Living Characters 2496.0 1986, October 1986.0
2 1458 Green Lantern (Hal Jordan) \/wiki\/Green_Lantern_(Hal_Jordan) Secret Identity Good Characters Brown Eyes Brown Hair Male Characters Living Characters 1565.0 1959, October 1959.0
3 1659 James Gordon (New Earth) \/wiki\/James_Gordon_(New_Earth) Public Identity Good Characters Brown Eyes White Hair Male Characters Living Characters 1316.0 1987, February 1987.0
4 1576 Richard Grayson (New Earth) \/wiki\/Richard_Grayson_(New_Earth) Secret Identity Good Characters Blue Eyes Black Hair Male Characters Living Characters 1237.0 1940, April 1940.0
... ... ... ... ... ... ... ... ... ... ... ... ...
6891 66302 Nadine West (New Earth) \/wiki\/Nadine_West_(New_Earth) Public Identity Good Characters NaN NaN Female Characters Living Characters NaN NaN NaN
6892 283475 Warren Harding (New Earth) \/wiki\/Warren_Harding_(New_Earth) Public Identity Good Characters NaN NaN Male Characters Living Characters NaN NaN NaN
6893 283478 William Harrison (New Earth) \/wiki\/William_Harrison_(New_Earth) Public Identity Good Characters NaN NaN Male Characters Living Characters NaN NaN NaN
6894 283471 William McKinley (New Earth) \/wiki\/William_McKinley_(New_Earth) Public Identity Good Characters NaN NaN Male Characters Living Characters NaN NaN NaN
6895 150660 Mookie (New Earth) \/wiki\/Mookie_(New_Earth) Public Identity Bad Characters Blue Eyes Blond Hair Male Characters Living Characters NaN NaN NaN

6896 rows × 12 columns

Methods

What is a method?

from dnaseq import DNASeq  # Importing the DNASeq class from the dnaseq module

# Example usage of the DNASeq class
dna_sequence = DNASeq("ATCGTAGCTAGCTAGCTAGCTAGCTAGCTAGC")
print(f"Original sequence: {dna_sequence.sequence}")  # Prints the DNA sequence

# Example reverse_compliment method call
print(
    f"Reverse compliment sequence: {dna_sequence.reverse_complement()}"
)  # Prints the reverse complement of the DNA sequence
Original sequence: ATCGTAGCTAGCTAGCTAGCTAGCTAGCTAGC
Reverse compliment sequence: GCTAGCTAGCTAGCTAGCTAGCTAGCTACGAT

Class Methods

What is a class method?

# remember I have already imported DNASeq above

# Example useage of from_rna class method
rna_sequence = DNASeq.from_rna("AUGCUAGCUAGCUAGCUAGCUAGCUAGCUAGC")
print(
    f"RNA sequence converted to DNA: {rna_sequence.sequence}"
)  # Prints the DNA sequence converted from RNA
RNA sequence converted to DNA: ATGCTAGCTAGCTAGCTAGCTAGCTAGCTAGC

Summary

Functions perform a computational task when called. Methods and class methods are more specialized functions associated with a class and are called differently. Remember, all methods are functions but not all functions are methods.

When to use functions

When to use methods

When to use class methods

comments powered by Disqus