class Item:
pay_rate = 0.8 # The pay after %20 discount
all = []
def __init__(self, name: str, price: float, quantity=0):
#Run validations to the recieved arguments
assert price >= 0, f"Price {price} is not greater than or equal tozero!"
assert quantity >= 0, f"Quantity {quantity} is not greater than or equal to zero!"
# Assign to self object
self.name = name
self.price = price
self.quantity = quantity
#Actions to execute
Item.all.append(self)
def calculate_total_price(self):
return self.price * self.quantity
def apply_discount(self):
self.price = self.price * self.pay_rate
@classmethod
def instantiate_from_csv(cls):
with open('items.csv', 'r') as f:
reader = csv.DictReader(f)
items = list(reader)
for item in items:
Item(
name=item.get('name'),
price=float(item.get('price')),
quantity=int(item.get('quantity')),
)
@staticmethod
def is_integer(num):
#We will count out the floats that are .0
if isinstance(num, float):
#Count out the floats that are point zero
return num.is_integer()
elif isinstance(num, int):
return True
else:
return False
def __repr__(self):
return f"Item('{self.name}', {self.price}, {self.quantity})"
Im currently learning python and trying to understand the OOP Concept.I understood it all except the following lines
def is_integer(num):
#We will count out the floats that are .0
if isinstance(num, float):
#Count out the floats that are point zero
return num.is_integer()
elif isinstance(num, int):
return True
else:
return False
Can someone explain me why num.is_integer() returns False? That function is defined for the purpose of removing .0 from floats like 100.0 or 50.0 (Tutorial says that)
It's my firs time encountering this type of return usage.I'm accustomed to return a*b or return 'Hi' type of stuff.
True, else it returnsFalse, also thatelse:is not necessary, can just unindentreturn Falseand removeelse:- Matiiss