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?
- A function is a coding operation that performs a task when called, independent of a class.
- example: pd.read_csv() – calling this pandas function reads in a file from the path provided.
- to call a pandas function, you first need to import with
import pandas as pd, since pandas is not default within python. - note: Without getting into too much detail, even using
+or other operators is calling 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?
- A method is a function of a class object that operates on the objects data.
- i.e. all methods are functions but not all functions are methods.
- Methods are functions that you will typically always need when using the class.
- calling structure: class_object.method()
- Again to call a method or use the class, you need to import your class.
- For this tutorial I will be using the class
DNASeq.
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?
- A class method is a function in a class that can be called outside of the class.
- These are useful in unique scenarios, such as an alternate way to create an object, that are useful when working with a class.
- calling structure: Class.class_method(arg)
# 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 you need to perform an operation that is independent of a specific data type.
- general utility operations that work across different contexts.
- example: reading in a csv file.
When to use methods
- When you want to perform a specific function on an object’s data.
- When the function operation is directly applicable to the class.
- reverse_complement()
When to use class methods
- When you need an additional way to create an object.
- When you need to perform operations related to the class itself rather than specific instances.
- example: reading RNA rather than DNA.