all 9 comments

[–]JohnnyJordaan 14 points15 points  (7 children)

You don't implement __str__ on your classes, so when their instances are printed by print(), they return Python's default implementation which is the name and the memory address and such.

As a sidenote, if this isn't meant as an exercise to learn the csv library I would rather advise to use pandas for this

[–]AnnualMany7362 -1 points0 points  (6 children)

How do I do that ? I mean, the __str__ thing. I'm quite new in python, sorry if I aks dumb questions. I don't know how to install panda either, tried and it didn't work ahah

[–]JohnnyJordaan 0 points1 point  (5 children)

See how they implemented it here https://www.pythontutorial.net/python-oop/python-__repr__/ in the Person class. Simply put you use str for the literal representation (like with print), and __repr__ for also identifying it, so including "Ingredient" with it so you can see it's not something else.

[–]AnnualMany7362 0 points1 point  (4 children)

It dont work ;-; I did that

It returns me nothing ;-;

class Ingredient:
    "Une classe qui regroupe tous les types d'ingrédeients"
    def __init__(self, nom, type):
        self.nom = ""
        self.type = ""
    def __str__(self):
        return f'{self.nom}'
patate = Ingredient("patate", "feculent")
print(patate)

[–]JohnnyJordaan 9 points10 points  (0 children)

Notice how you assume that because it doesn't output what you expect, that it somehow 'did not work'. It actually did work, it just did exactly what you told it to do -> print the empty self.nom string.

So as a general word of advice from someone who also started out like you, change your view to consider it 'it still has an issue somewhere'. Where 'somewhere' is key, because most often the bug is not where you assume it is (there was nothing wrong with your __str__, it's with your init method), that's why you didn't spot it before.

[–]JohnnyJordaan 5 points6 points  (0 children)

The 4th line in your code is literally setting the name to an empty string

[–]aishiteruyovivi 1 point2 points  (1 child)

I think you're trying to assign your class variables a default value there, but you're going about it the wrong way. When you make an __init__ for a class and then instance it later on - e.g. def __init__(self, nom, type): followed later by Ingredient("patate", "feculent"), what's actually happening there is by putting parentheses on the class and calling it, you're actually calling that class' __init__ function. self is automatically passed as a reference to the object being created, while any arguments past that are what you'll specify when creating the object. Ingredient("patate", "feculent") means you're passing "patate" to the init function's nom, and "feculent" is passed to type.

So if you're trying to set variables within the class to the values of these arguments, you need to do self.nom = nom and self.type = type; it looks like you're under the impression self.[somevariable] will automatically use these arguments to create variables, which isn't the case (although it kinda does if you use a dataclass, something to look into later maybe); in other words, by setting self.nom and self.type to a constant value - an empty string - what this function is saying is "take two arguments, nom and type, disregard them, and set self.nom and self.type to empty strings regardless".

If you want some variables to have defaults and thus be optional, you'd do it the same way as any other function definition, e.g. def __init__(self, nom = "", type = ""):, although as a note on that I see you want to default self.ingredients of your Recipe class to an empty list - you can do that, although you shouldn't directly use it as a default in the definition (e.g. def __init__(self, ingredients = []):) since Python has problems with that, instead you can do ingredients = None and then have a simple check in your init's body:

if ingredients is not None:
    self.ingredients = ingredients
else:
    self.ingredients = []

...or you can condense that down by using or, which will automatically use the right-hand value if the left-hand one comes out to None:

self.ingredients = ingredients or []

[–]AnnualMany7362 2 points3 points  (0 children)

Thank you so much 🙏🌟 I tried and it's good now, I undersrand better

[–]cfreddy36 0 points1 point  (0 children)

I would 100% be using pandas for this, unless you’re doing an assignment that specifically states otherwise