Lesson · 40 min · Free
Python Classes & OOP Essentials
Python Classes & OOP Essentials Python Classes & OOP Essentials Welcome to this crucial module on Python Classes and Object-Oriented Programming (OOP) essentials. For pharmacy and biotech students, understanding OOP is n
Python Classes & OOP Essentials
Welcome to this crucial module on Python Classes and Object-Oriented Programming (OOP) essentials. For pharmacy and biotech students, understanding OOP is not just a theoretical exercise; it's a fundamental paradigm that underpins much of the software used in drug discovery, bioinformatics, and laboratory automation. OOP allows us to model real-world entities and their interactions more intuitively, leading to more organized, reusable, and maintainable code. Think about modeling a "Drug Molecule" with its properties (chemical formula, molecular weight, efficacy) and behaviors (binding to a receptor, metabolizing). OOP provides the tools to build such representations. At its core, OOP revolves around the concept of "objects." An object is an instance of a "class." A class can be thought of as a blueprint or a template for creating objects. It defines the characteristics (attributes or data) and behaviors (methods or functions) that all objects of that class will possess. For example, a Patient class might have attributes like patient_id , name , and medications , and methods like administer_drug() or record_vitals() .
Defining Classes and Creating Objects
Let's start by defining a simple class in Python. We use the class keyword followed by the class name. Class names typically follow CamelCase convention. Inside the class, we define its attributes and methods. A special method, __init__ , is the constructor. It's automatically called when a new object (instance) of the class is created. The self parameter in methods refers to the instance of the class itself, allowing us to access its attributes and other methods. class DrugMolecule: def __init__(self, name, chemical_formula, molecular_weight): self.name = name self.chemical_formula = chemical_formula self.molecular_weight = molecular_weight self.binding_affinity = None # An attribute that can be set later def display_info(self): print(f"Drug Name: {self.name}") print(f"Chemical Formula: {self.chemical_formula}") print(f"Molecular Weight: {self.molecular_weight} g/mol") if self.binding_affinity: print(f"Binding Affinity: {self.binding_affinity}") def set_binding_affinity(self, affinity_value): self.binding_affinity = affinity_value print(f"Binding affinity for {self.name} set to {affinity_value}.") # Creating objects (instances) of the DrugMolecule class aspirin = DrugMolecule("Aspirin", "C9H8O4", 180.16) paracetamol = DrugMolecule("Paracetamol", "C8H9NO2", 151.16) # Accessing attributes print(f"Aspirin's molecular weight: {aspirin.molecular_weight}") # Calling methods aspirin.display_info() paracetamol.set_binding_affinity("High") paracetamol.display_info() In the example above, DrugMolecule is our class. aspirin and paracetamol are two distinct objects created from this class. Each object has its own set of attribute values (e.g., aspirin.name is "Aspirin", while paracetamol.name is "Paracetamol"). The methods ( display_info , set_binding_affinity ) operate on the specific object they are called upon. OOP also emphasizes concepts like encapsulation, inheritance, and polymorphism. Encapsulation refers to bundling data (attributes) and methods that operate on the data within a single unit (the class). This helps in controlling access to data and preventing unintended modifications. While Python doesn't enforce strict private attributes like some other languages, convention dictates using a single underscore ( _attribute ) for protected attributes and double underscores ( __attribute ) for name-mangled (effectively private) attributes. class PatientRecord: def __init__(self, patient_id, name, dob): self.__patient_id = patient_id # "Private" attribute self.name = name self._dob = dob # Protected attribute def get_patient_id(self): return self.__patient_id def update_name(self, new_name): self.name = new_name print(f"Patient {self.__patient_id}'s name updated to {self.name}.") # Creating a patient object patient1 = PatientRecord("P001", "Alice Smith", "1990-05-15") # Accessing public attribute print(f"Patient name: {patient1.name}") # Trying to directly access "private" attribute (will work but discouraged) # print(patient1.__patient_id) # This will raise an AttributeError if accessed directly like this print(f"Patient ID via getter: {patient1.get_patient_id()}") # Modifying attribute patient1.update_name("Alicia Smith") print(f"Updated patient name: {patient1.name}") In the PatientRecord example, __patient_id is intended to be accessed and modified only through methods, demonstrating a form of encapsulation. This helps maintain data integrity, which is critical in healthcare and scientific data management.
Key Takeaways
Classes are blueprints for creating objects, defining their attributes (data) and methods (behavior). Objects are instances of a class, representing real-world entities. The __init__ method is the constructor, called when an object is created. The self parameter refers to the instance of the class. Encapsulation is the bundling of data and methods within a class, promoting data integrity and modularity. OOP facilitates modular, reusable, and maintainable code, essential for complex scientific applications.
Practice Exercise
Create a Python class named LaboratoryInstrument . This class should have attributes for instrument_id , model_name , manufacturer , and calibration_date . Include a method perform_maintenance() that updates the calibration_date to the current date and prints a message indicating that maintenance was performed. Then, create two instances of LaboratoryInstrument , set their initial attributes, display their information, and call the perform_maintenance() method on one of them.
Watch the full lesson — free
This topic is part of Python Programming - Basics, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →