Seminar 007: Extending Methods¶
Method decorators¶
Instance Methods¶
Instance methods are functions defined within a class that operate on instances of that class. They have access to the instance through the self parameter, which allows them to read or modify the instance’s attributes and call other instance methods. These methods are used when actions are performed that relate to the individual instance, such as changing an instance’s state or interacting with its attributes.
Class Methods¶
Class methods are functions that operate on the class itself rather than on instances of the class. They are identified by the @classmethod
decorator and use cls as the first parameter, which refers to the class. Class methods can access and modify class-level attributes, which are shared across all instances of the class. These methods are useful for operations that are related to the class as a whole, such as creating instances in different ways or maintaining a count of all instances
Static Methods¶
Static methods are functions defined within a class that do not operate on an instance or class directly. They are identified by the @staticmethod
decorator and do not take self or cls as a parameter. Static methods behave like regular functions but are included in the class’s namespace. They are used for utility functions that perform a task related to the class but do not need to access or modify the class or instance state.
class DNASequence:
def __init__(self, ID, sequence):
"""This is a class to define a DNA sequence.
Args:
ID (str): The identifier of this sequence.
sequence (str): The sequence of this gene.
"""
self.ID = ID
self.sequence = sequence
self.length = len(self.sequence)
def get_sequence(self):
return self.sequence
@staticmethod
def concat_sequences(sequence1, sequence2):
return sequence1 + sequence2
@classmethod
def from_special_format(cls, content: str):
# "1024\nAGAGGATGATC"
ID, sequence = content.split("|")
# Equivalent to: DNASequence(ID, sequence)
return cls(ID, sequence)
Usage examples¶
# Instance methods operate on the object instance
# --> getSequence is an instance method
instance1 = DNASequence("Instance1", "ATG")
instance2 = DNASequence("Instance2", "TGA")
# The instance method will return the specific
# content of the particular instance where we
# are calling the method from
print(f"Instance 1: {instance1.get_sequence()}")
print(f"Instance 2: {instance1.get_sequence()}")
Instance 1: ATG Instance 2: ATG
# Static methods are basically functions attached
# to a class. These do NOT operate on an instance
# and hence the 'self' parameter is not necessary
# Static method are mostly used for utilities or
# helper methods within a class
instance = DNASequence("Instance", "ATG")
instance.concat_sequences("ATG", "TGA")
'ATGTGA'
# Classmethods operate on classes and are meant to
# produce an instance in a special way. The 'cls' arg
# is basically the class itself and we can decide how
# to initialize the class within this method
# These methods are usually used to wrap complex alternative
# initialization methods. For instance, if we take a single
# FASTA entry, we could extract ID and sequence in a single step
instance = DNASequence.from_special_format("ID|ATGATG")
instance.__dict__
{'ID': 'ID', 'sequence': 'ATGATG', 'length': 6}