Python Classes, Objects, and Inheritance Fundamentals
Classified in Computers
Written on  in 
English with a size of 4.11 KB
Understanding Objects in Programming
An object is a software entity that contains data (attributes) and methods. It represents a real-world entity that can be distinctly identified.
Every object has a unique:
- Identity: The name of the object (e.g., the variable name).
 - State: The data stored in the object, which defines its properties.
 - Behavior: The actions an object can perform, defined by its methods.
 
Can an object be passed as an argument to a function?
Yes. In Python, objects are passed by reference. This means any changes made to the object's attributes within the function will permanently alter the original object. This behavior is similar to how lists and dictionaries are handled. Think of it as sharing a key to a single locker rather than getting a copy of its contents.
Classes, Objects, and Inheritance
Consider a Student class as a blueprint. We can create specialized subclasses like AccountingStudent and BBAStudent that inherit from it.
- Shared Attributes (from 
Student): UT EID, Email, Phone Number. - Different Behaviors: Each subclass might have a different set of required classes to take based on the major.
 
Each object created from these classes will have its own unique identity, state, and behavior.
What is the purpose of UML Class Diagrams in OOP?
The main purpose of a Unified Modeling Language (UML) Class Diagram is to serve as visual documentation. It helps developers illustrate and design the structure of the classes within a system, showing their attributes, methods, and relationships.
Python Class Syntax Summary
Defining a Class
The self parameter is required in every method and refers to the specific instance of the object the method is operating on.
# class_definition.py
class ClassName:
    # The __init__ method is the constructor, called automatically when an object is created.
    def __init__(self, value1, value2):
        # Public attribute
        self.attribute = value1
        # Private attribute (indicated by a double underscore)
        # Cannot be accessed directly from outside the class.
        self.__private_attribute = value2
    # A standard method that defines the object's behavior.
    def method_name(self):
        # statement(s)
        pass
    # The __str__ method returns a human-readable string summary of the object.
    def __str__(self):
        summary = f'Attribute 1: {self.attribute}, Attribute 2: {self.__private_attribute}'
        return summaryAccessors (Getters)
An accessor is a method used to retrieve the value of a private data attribute from an object. It is a value-returning function.
# Inside the ClassName class
def get_private_attribute(self):
    return self.__private_attributeMutators (Setters)
A mutator is a method used to change the value of a private data attribute in an object. Mutators require a parameter and typically do not return a value.
# Inside the ClassName class
def set_private_attribute(self, value):
    self.__private_attribute = valueCreating and Using an Object
To use a class, you first create an instance (or object) of that class.
# main.py
# Assuming the class is in a file named class_definition.py
import class_definition
def main():
    # Create two instances of the ClassName class.
    # This calls the __init__ method for each object.
    object_name1 = class_definition.ClassName("public_val1", "private_val1")
    object_name2 = class_definition.ClassName("public_val2", "private_val2")
    # Access a public attribute of an object.
    print(object_name1.attribute)
    # Call a method of an object.
    object_name1.method_name()
    # Printing an object implicitly calls its __str__() method.
    print(object_name1)
# Run the main function
if __name__ == "__main__":
    main()