Defining Public Methods
Public methods are the core of a class's public interface. They are the entry points for interacting with the class and performing its intended functionality. When defining public methods, it's important to consider the following guidelines:
Identify the Core Functionality
Carefully examine the purpose and responsibilities of your class, and identify the key operations that should be exposed as public methods. These methods should represent the core functionality that the class is designed to provide.
class BankAccount:
def __init__(self, account_number, balance):
self._account_number = account_number
self._balance = balance
def deposit(self, amount):
"""
Deposit the specified amount into the account.
Args:
amount (float): The amount to be deposited.
Returns:
float: The updated account balance.
"""
self._balance += amount
return self._balance
def withdraw(self, amount):
"""
Withdraw the specified amount from the account.
Args:
amount (float): The amount to be withdrawn.
Returns:
float: The updated account balance.
"""
if self._balance >= amount:
self._balance -= amount
return self._balance
else:
return self._balance
In the example above, the deposit()
and withdraw()
methods represent the core functionality of the BankAccount
class, allowing users to manage their account balance.
Use Clear and Meaningful Names
Choose intuitive and descriptive names for your public methods to make their purpose obvious. Avoid using abbreviations or cryptic names that may confuse the users of your class.
class Rectangle:
def __init__(self, width, height):
self._width = width
self._height = height
def get_area(self):
"""
Calculate and return the area of the rectangle.
Returns:
float: The area of the rectangle.
"""
return self._width * self._height
def get_perimeter(self):
"""
Calculate and return the perimeter of the rectangle.
Returns:
float: The perimeter of the rectangle.
"""
return 2 * (self._width + self._height)
In the example above, the get_area()
and get_perimeter()
methods clearly indicate their purpose, making it easy for users to understand how to interact with the Rectangle
class.
Provide Appropriate Documentation
Use docstrings and comments to explain the purpose, expected inputs, and expected outputs of your public methods. This documentation will help users of your class understand how to use the methods correctly.
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
def get_name(self):
"""
Retrieve the name of the person.
Returns:
str: The name of the person.
"""
return self._name
def get_age(self):
"""
Retrieve the age of the person.
Returns:
int: The age of the person.
"""
return self._age
def set_age(self, new_age):
"""
Update the age of the person.
Args:
new_age (int): The new age to be set.
"""
self._age = new_age
In the example above, the docstrings provide clear explanations of the purpose, inputs, and outputs of the public methods, making it easier for users to understand how to interact with the Person
class.
By following these guidelines, you can define a well-designed public interface for your Python classes, making them more intuitive, maintainable, and easier to use.