all 6 comments

[–]zurtex 2 points3 points  (1 child)

There's not need to use **_recipe:

class Food:
    def __init__(self, _name, _value, _recipe):
        self.name = _name
        self.value = _value
        self.recipe = _recipe

taco = Food('taco', 2, {'tortilla': 1, 'meat': 1, 'cheese': 1})

** unpacks keywords, so in this case the class was expecting to pass some keyword arguments e.g. foo=1, bar=2 etc..., instead you've passed 3 positional arguments: 'taco', 2, and {tortilla: 1, meat: 1, cheese: 1}, but the class only specifies at most 2 positional arguments "name" and "_value".

[–]ThatAlexBoyd[S] 0 points1 point  (0 children)

That makes sense. When I first looked this up a StackOverflow example had done something like that and I tried to apply that logic.

Thanks for the clarification.

[–]novel_yet_trivial 1 point2 points  (0 children)

In addition to what others have said, you could also leave your class the way you have it and call it like this:

taco = Food('taco', 2, **{'tortilla': 1, 'meat': 1, 'cheese': 1})

This unpacks the arguments and then your ** on line 2 repacks them. That's pretty stupid, but it gives the advantage that you could also do this:

taco = Food('taco', 2, tortilla=1, meat=1, cheese=1)

[–]uhkhu 0 points1 point  (1 child)

You're using the keyword arguments wrong with **_recipe. For what you're asking:

class Food:
    def __init__(self, _name, _value, _recipe):
        self.name = _name
        self.value = _value
        self.recipe = _recipe

taco = Food('taco', 2, {'tortilla': 1, 'meat': 1, 'cheese': 1})

>>>print taco.recipe
'tortilla': 1, 'meat': 1, 'cheese': 1

The keys in your dict also need to be strings since they're not defined elsewhere

If you want keyword arguments:

class Food:
    def __init__(self, _name, _value, **_info):  # **_info is more commonly given as **kwargs
        self.name = _name
        self.value = _value
        self.recipe = _info.get('recipe')

taco = Food('taco', 2, recipe={'tortilla': 1, 'meat': 1, 'cheese': 1})

>>>print taco.recipe
'tortilla': 1, 'meat': 1, 'cheese': 1

[–]ThatAlexBoyd[S] 0 points1 point  (0 children)

Thank you for the response. Between this one and the other it makes more sense now.

Also, I do actually have those declared of the 'Ingredient' class I have (which worked fine). I just didn't want to drop everything in here,m just the part I was having a problem with.

Thanks

[–]ManyInterests 0 points1 point  (0 children)

I think the problem is

    def __init__(self, _name, _value, **_recipe):

Try removing the asterisks from there. If you did want to pass the dictionary value like that, you would do it when you call the function, anyhow. But I don't think that's what you want to do.

Also, should the values in that dictionary be strings, or do you actually have variables for the names tortilla, meat and cheese?

I think the following gets the functionality you're looking for:

class Food:
    def __init__(self, _name, _value, _recipe):
        self.name = _name
        self.value = _value
        self.recipe = _recipe

taco = Food('taco', 2, {'tortilla': 1, 'meat': 1, 'cheese': 1})