This issue doesn't really have to do with pickling specifically.
Consider:
def return_a_tuple(a, b, c):
return (a, (b, c))
x = return_a_tuple(1, 2, 3)
How do you extract a
, b
, and c
from x
?
The answer, as jonrsharpe says, is to do something like:
(a, (b, c)) = x
Also, I suspect that instead of
recipedetails.append((name, amount, unit))
recipeinfo = serve , recipedetails
You meant something like
recipedetail = (name, amount, unit)
recipedetails.append(recipedetail)
recipeinfo = serve, repcipedetail
Your original code is creating pairs of serve
with a list of every recipe detail you've seen so far, instead of only the one you've just read in.
Alternatively, you could consider using a namedtuple
:
from collections import namedtuple
RecipeDetail = namedtuple('RecipeDetail', ['name', 'amount', 'unit'])
RecipeInfo = namedtuple('RecipeInfo', ['serve', 'recipe_detail'])
You can then do something like
recipedetail = RecipeDetail(name, amount, unit)
recipedetails.append(recipedetail)
recipeinfo = RecipeInfo(serve, recipedetail)
Now you can access sub-fields by doing:
recipedetail.name
recipeinfo.recipe_detail.amount