Update Swap.py using best practices and standards of python. #1941
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The improvements made to the program are significant and can be summarized as follows:
Object-Oriented Approach: The original code was a simple script with variables and direct swapping of values. In contrast, the improved version uses an object-oriented approach with a class Swapper that encapsulates the swapping logic and functionality.
Class Methods: The swapping methods (swap_tuple_unpacking, swap_temp_variable, and swap_arithmetic_operations) are defined as class methods in the Swapper class. This allows them to be called on instances of the class and operate on the specific values of x and y associated with each instance.
Error Handling: The init method in the improved version includes error handling to check if both x and y are integers. If not, a ValueError is raised, providing a clear message to the user about the expected input.
Encapsulation: The display of values before and after swapping is encapsulated within the display_values method. This method allows for consistent and easy printing of values without repeating the print statements in each swapping method.
Docstrings: The improved version includes detailed docstrings for the class and each method. Docstrings provide clear documentation about the purpose and usage of the class and its methods, making it easier for other developers (including the original programmer) to understand and use the code.
Examples with Class Methods: The improved version demonstrates the usage of class methods with two examples (swapper1 and swapper2). Each example creates an instance of Swapper with different initial values and then calls one of the swapping methods on the instance.
Reusability: With the improved version, the Swapper class can be easily reused in other parts of the code or in different programs, promoting code modularity and reusability.
Overall, the improved version is more organized, follows object-oriented principles, and provides better readability and maintainability compared to the original script-like code. It also incorporates error handling and documentation, making it more robust and user-friendly.