With the `AddSub` class, you can switch between addition and subtraction operations dynamically, providing flexibility for various scenarios requiring different operations.
Project description
Python Class for Dynamically Switching Between Addition and Subtraction Operations: AddSub
Introduction
In this tutorial, we will create a Python class named AddSub
that allows us to dynamically switch between addition and subtraction operations. This class provides a flexible way to perform different operations on numbers based on a specified method.
Code Implementation
class AddSub:
def __init__(self, method_val: int = 1) -> None:
"""
This is the constructor of the AddSub class.
It takes an optional parameter method_val, which specifies the default method to be used for operations.
If method_val is not an integer, it is set to 1 (addition) by default.
If method_val is less than 0 or greater than 1, it is also set to 1 by default.
"""
if type(method_val) != type(1):
self.method = 1
print('only integer elements accepted')
elif ((method_val <= 1) and (method_val >= 0)):
self.method = method_val
else:
self.method = 1
print("Please enter a valid method by default addition operation has set")
def operation(self, first_number: int = 0, second_number: int = 0) -> int:
"""
This method performs the operation specified by the method attribute on the two given numbers.
If the method attribute is 0, subtraction is performed.
If the method attribute is 1, addition is performed.
"""
if (self.method == 0):
return first_number - second_number
elif (self.method == 1):
return first_number + second_number
def change_method(self, method_val: int) -> None:
"""
This method changes the method attribute to the given value.
If the given value is not an integer, the method attribute is not changed.
If the given value is less than 0 or greater than 1, the method attribute is also not changed.
"""
if (type(method_val) != type(1)):
print('only integer elements accepted, previous method has unchanged')
elif ((method_val <= 1) and (method_val >= 0)):
self.method = method_val
else:
print("Please enter a valid method, previous method has unchanged")
# Usage:
# Creating an instance of the AddSub class
add_sub = AddSub()
# Performing addition using the default method
result = add_sub.operation(10, 20)
print("Addition result:", result) # Output: 30
# Changing the method to subtraction
add_sub.change_method(0)
# Performing subtraction using the new method
result = add_sub.operation(20, 10)
print("Subtraction result:", result) # Output: 10
# Changing the method to an invalid value
add_sub.change_method(2)
# Performing addition using the previous method (addition)
result = add_sub.operation(100, 200)
print("Addition result (using previous method):", result) # Output: 300
Explanation
- The
__init__
method is the constructor of theAddSub
class. It takes an optional parametermethod_val
that specifies the default method to be used for operations. - In the
__init__
method, we check ifmethod_val
is an integer. If it is not, we print an error message and setmethod
to 1 (addition). - We also check if
method_val
is within the valid range of 0 (subtraction) and 1 (addition). If it is not, we print an error message and setmethod
to 1. - The
operation
method takes two integer parametersfirst_number
andsecond_number
and performs the operation specified by themethod
attribute on the two given numbers. - The
change_method
method takes an integer parametermethod_val
and changes themethod
attribute to the given value, provided that it is a valid value. - In the usage section, we create an instance of the
AddSub
class and perform addition and subtraction operations using the default method. - We then change the method to subtraction using the
change_method
method and perform a subtraction operation. - We try to change the method to an invalid value, but the
change_method
method prevents this and prints an error message. - Finally, we perform an addition operation using the previous method (addition) and demonstrate that the method has not changed.
Conclusion
The AddSub
class provides a flexible way to dynamically switch between addition and subtraction operations. This can be useful in various scenarios where you need to perform different operations based on certain conditions or user input.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file addition_subtraction-0.1.1.tar.gz
.
File metadata
- Download URL: addition_subtraction-0.1.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.11 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ef84da633cdb925fc89ea22c2211de8adfd79324d94228739c9f47956b1c14f |
|
MD5 | 2353c9f87bf73f31b6e35cc0fe374ecc |
|
BLAKE2b-256 | a00a13adc768fcd5f35a4720af0db8d7b8d5d66586fdfdc93aaab8bf67170c72 |
File details
Details for the file addition_subtraction-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: addition_subtraction-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.11 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5314ae3974571f52d134d4efeae90bcba3af8b01079458db65b42a6e6d4e735b |
|
MD5 | 3e62caa2d35d21374974993196aa6782 |
|
BLAKE2b-256 | 78ec3f451ee6b0347cfa4e9f126614b01727ecc0318082aefbdbfd3813cccc40 |