I'm trying to learn object orientated programming and when setting an attribute why does it return None
?
Any tips on writing better code also appreciated... This is my class:
BEANS = ["black", "pinto"]
RICES = ["brown", "white"]
MEATS = ["chicken", "pork", "steak", "tofu"]
class Burrito:
def __init__(self,
meat,
to_go,
rice,
beans,
extra_meat=False,
guacamole=False,
cheese=False,
pico=False,
corn=False
):
self.meat = self.setter("meat",meat)
self.rice = self.setter("rice",rice)
self.beans = self.setter("beans",beans)
self.to_go = False
self.extra_meat = False
self.guacamole = False
self.cheese = False
self.pico = False
self.corn = False
def setter(self,category,attribute):
print("category: ",category)
print("attribute: ",attribute)
if category == "meat":
if attribute in MEATS:
self.meat = attribute
else:
self.meat = False
if category == "rice":
if attribute in RICES:
self.rice = attribute
else:
self.rice = False
if category == "beans":
if attribute in BEANS:
self.beans = attribute
else:
self.beans = False
else:
self.category = "Error"
When I run this I expected the print to be False
but returning None
noodle_burrito = Burrito("spagetti", True, True, False)
print("noodle_burrito.meat: ",noodle_burrito.meat)
And when I run this I expected the print to be tofu
but returning None
vegg_burrito = Burrito("tofu", True, True, False)
print("vegg_burrito.meat: ",vegg_burrito.meat)
Thanks for any learning tips! Trying to reference this other SO post but I have never used super
before.