You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is an example of implementing a NameTuple from the typing module.
fromtypingimportNamedTuple, TupleclassFood(NamedTuple):
""" Food parameters name (str): name of the food protein (Tuple(float, str)): amount and unit e.g. 10 g carbohydrate (Tuple(float, str)): amount and unit e.g. 10 g fat (Tuple(float, str)): amount and unit e.g. 10 g Example egg = Food("egg", (10, g), # protein 10 grams (10, g), # carbohydrate (10, g), # fat ) """name: strprotein: Tuple[float, str]
carbohydrate: Tuple[float, str]
fat: Tuple[float, str]
def__repr__(self):
returnf"""{self.name}{'-'*len(self.name)}{'Protein'.ljust(15)}{self.protein[0]} ({self.protein[1]}){'Carbohydrate'.ljust(15)}{self.carbohydrate[0]} ({self.carbohydrate[1]}){'Fat'.ljust(15)}{self.fat[0]} ({self.fat[1]}) """
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Here is an example of implementing a
NameTuple
from thetyping
module.This is an alternative to a dictionary to store 10 common foods. Let me know what you think.
Beta Was this translation helpful? Give feedback.
All reactions