def __bool__ oriented object programming python
Question:
I try to create customer who comes often to a restaurant. Then, because of their budget they can maybe not come to the restaurant. I decided to use the __bool__
statement provided by Python but it always gives me true as output.
I used the following code:
prices_drink = {"milkshake": 5,"frappucino": 4, "water": 2,"coffee": 3, "soda": 3, "tea": 3}
prices_food = {"sandwich": 2, "cookie": 2, "pie": 3,"muffin": 3}
class Customer (object):
def __init__(self, id, budget):
self.id = id
self.budget = budget
class Recurrent(Customer):
def __init__(self, id, budget):
self.id = id
self.budget = budget
def __bool__(self):
if self.budget <= max(prices_drink.values()) + max(prices_food.values()):
return False
return True
def description(self):
print(f"This is Customer {self.id}. They come here often. Can he buy? {bool(Recurrent)}")
customer2 = Recurrent(123456, 5)
customer2.description()
What is the solution?
Answers:
If you call bool on Recurrent you call it on the class, not the object.
If you need to do it the way you did it for school, thats okay. But I would suggest to use method names that make sense and refactor id to customer_id as id is a built-in function in python
And as us are using a parent class you can use super to call the constructor of the super class and forward the arguments to it.
For example:
prices_drink = {"milkshake": 5, "frappucino": 4, "water": 2, "coffee": 3, "soda": 3, "tea": 3}
prices_food = {"sandwich": 2, "cookie": 2, "pie": 3, "muffin": 3}
class Customer(object):
def __init__(self, customer_id, budget):
self.customer_id = customer_id
self.budget = budget
class Recurrent(Customer):
def __init__(self, customer_id, budget):
super().__init__(customer_id, budget)
def is_in_budget(self):
if self.budget <= max(prices_drink.values()) + max(prices_food.values()):
return False
return True
def print_customer_liquidity(self):
print(
f'This is Customer {self.customer_id}.'
f' They come here often. Can he buy ? {self.is_in_budget()}'
)
customer2 = Recurrent(123456, 5)
customer2.print_customer_liquidity()
I try to create customer who comes often to a restaurant. Then, because of their budget they can maybe not come to the restaurant. I decided to use the __bool__
statement provided by Python but it always gives me true as output.
I used the following code:
prices_drink = {"milkshake": 5,"frappucino": 4, "water": 2,"coffee": 3, "soda": 3, "tea": 3}
prices_food = {"sandwich": 2, "cookie": 2, "pie": 3,"muffin": 3}
class Customer (object):
def __init__(self, id, budget):
self.id = id
self.budget = budget
class Recurrent(Customer):
def __init__(self, id, budget):
self.id = id
self.budget = budget
def __bool__(self):
if self.budget <= max(prices_drink.values()) + max(prices_food.values()):
return False
return True
def description(self):
print(f"This is Customer {self.id}. They come here often. Can he buy? {bool(Recurrent)}")
customer2 = Recurrent(123456, 5)
customer2.description()
What is the solution?
If you call bool on Recurrent you call it on the class, not the object.
If you need to do it the way you did it for school, thats okay. But I would suggest to use method names that make sense and refactor id to customer_id as id is a built-in function in python
And as us are using a parent class you can use super to call the constructor of the super class and forward the arguments to it.
For example:
prices_drink = {"milkshake": 5, "frappucino": 4, "water": 2, "coffee": 3, "soda": 3, "tea": 3}
prices_food = {"sandwich": 2, "cookie": 2, "pie": 3, "muffin": 3}
class Customer(object):
def __init__(self, customer_id, budget):
self.customer_id = customer_id
self.budget = budget
class Recurrent(Customer):
def __init__(self, customer_id, budget):
super().__init__(customer_id, budget)
def is_in_budget(self):
if self.budget <= max(prices_drink.values()) + max(prices_food.values()):
return False
return True
def print_customer_liquidity(self):
print(
f'This is Customer {self.customer_id}.'
f' They come here often. Can he buy ? {self.is_in_budget()}'
)
customer2 = Recurrent(123456, 5)
customer2.print_customer_liquidity()