Re: [Tutor] Hi everybody stuck on some error need help please thank you!!
(Don't top-post. Either put your remarks immediately after the part they reference, or at the end of the message. Otherwise, everything's thoroughly out of order.) Marco Rompré wrote: I tried to enter model = Modele (nom_fichier) but it still does not work. You didn't define the global nom_fichier till after that line. In general, while you're learning, please avoid using the same names for global values, class attributes, instance attributes, function parameter names, and local variables. The rules for what a name means changes depending on where the name is used. On Fri, Apr 23, 2010 at 11:22 PM, Steven D'Aprano wrote: On Sat, 24 Apr 2010 01:07:11 pm Marco Rompré wrote: Here's my code: [...] class Modele: """ La definition d'un modele avec les magasins. """ def __init__(self, nom_fichier, magasins =[]): self.nom_fichier = nom_fichier self.magasins = magasins [...] if __name__ == '__main__': modele = Modele() This is where you got the error, because there's a required argument, for parameter nom_fichier. So you could use modele = Modele("thefile.txt") nom_fichier = "magasinmodele.txt" I'd call this something else, like g_nom_fichier. While you're learning, you don't want to get confused between the multiple names that look the same. modele.charger(nom_fichier) if modele.vide(): modele.initialiser(nom_fichier) modele.afficher() And here's my error : Traceback (most recent call last): File "F:\School\University\Session 4\Programmation SIO\magasingolfmodele.py", line 187, in modele = Modele() TypeError: __init__() takes at least 2 arguments (1 given) You define Modele to require a nom_fichier argument, but then you try to call it with no nom_fuchier. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi everybody stuck on some error need help please thank you!!
On Sat, 24 Apr 2010 01:33:46 pm Marco Rompré wrote: > I tried to enter model = Modele (nom_fichier) but it still does not > work. What does "still does not work" mean? Please copy and paste the error you get. > And for the list I don't understand very well, Open an interactive session and do some experiments. >>> a = [] # One empty list. >>> b = [] # A different empty list. >>> a.append(1) # Appends to the first list. >>> print a, b [1] [] >>> >>> a = [] # An empty list. >>> b = a # The SAME empty list. >>> a.append(1) >>> print a, b [1] [1] When you have a default value like magasins=[], it is like the second, not the first. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi everybody stuck on some error need help please thank you!!
I tried to enter model = Modele (nom_fichier) but it still does not work. And for the list I don't understand very well, Do you know where I can pay someone to help with my programming. Because I feel to annoy tutors with my basic stuff On Fri, Apr 23, 2010 at 11:22 PM, Steven D'Aprano wrote: > On Sat, 24 Apr 2010 01:07:11 pm Marco Rompré wrote: > > > Here's my code: > [...] > > class Modele: > > """ > > La definition d'un modele avec les magasins. > > """ > > def __init__(self, nom_fichier, magasins =[]): > > self.nom_fichier = nom_fichier > > self.magasins = magasins > [...] > > if __name__ == '__main__': > > modele = Modele() > > nom_fichier = "magasinmodele.txt" > > modele.charger(nom_fichier) > > if modele.vide(): > > modele.initialiser(nom_fichier) > > modele.afficher() > > > > And here's my error : > > > > Traceback (most recent call last): > > File "F:\School\University\Session 4\Programmation > > SIO\magasingolfmodele.py", line 187, in > > modele = Modele() > > TypeError: __init__() takes at least 2 arguments (1 given) > > > You define Modele to require a nom_fichier argument, but then you try to > call it with no nom_fuchier. > > > Also, I see that you do this: > > def __init__(self, nom_fichier, magasins =[]): > > You probably shouldn't do this -- it doesn't do what you probably think > it does. > > You probably think that what happens is that if you call > Modele(nom_fichier), the magasins attribute will be set to an empty > list. But that's not what happens. > > Default values in Python are defined when the method is created, not > when it is run. Watch this example: > > >>> class Test: > ... def __init__(self, x=[]): > ... self.x = x > ... > >>> a = Test() > >>> a.x > [] > >>> b = Test() > >>> b.x > [] > >>> > >>> a.x.append("Surprise!") > >>> b.x > ['Surprise!'] > > > How does this happen? Because every instance shares the same default > list. When you append to it, all the other instances see the same > change. > > You don't notice this with default values that are strings or numbers, > because you can't modify them, only replace them: > > >>> x = y = 2 # Make two variables that point to the same value. > >>> x is y # Make sure they are identical, not just equal. > True > >>> x = 3 # Make x point to something else. > >>> x is y # And no longer identical. > False > >>> > >>> x = y = [] # Make two variables that point to the same thing. > >>> x is y > True > >>> x.append('something') # Modify that list in place. > >>> x is y # Still identical. > True > >>> y > ['something'] > > > If this is the behaviour you want, then you don't need to do anything. > Otherwise you need to move the creation of the empty list inside the > method: > >def __init__(self, nom_fichier, magasins=None): >if magasins is None: > magasins = [] >self.nom_fichier = nom_fichier >self.magasins = magasins > > > > -- > Steven D'Aprano > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Marc-O. Rompré ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi everybody stuck on some error need help please thank you!!
On Sat, 24 Apr 2010 01:07:11 pm Marco Rompré wrote: > Here's my code: [...] > class Modele: > """ > La definition d'un modele avec les magasins. > """ > def __init__(self, nom_fichier, magasins =[]): > self.nom_fichier = nom_fichier > self.magasins = magasins [...] > if __name__ == '__main__': > modele = Modele() > nom_fichier = "magasinmodele.txt" > modele.charger(nom_fichier) > if modele.vide(): > modele.initialiser(nom_fichier) > modele.afficher() > > And here's my error : > > Traceback (most recent call last): > File "F:\School\University\Session 4\Programmation > SIO\magasingolfmodele.py", line 187, in > modele = Modele() > TypeError: __init__() takes at least 2 arguments (1 given) You define Modele to require a nom_fichier argument, but then you try to call it with no nom_fuchier. Also, I see that you do this: def __init__(self, nom_fichier, magasins =[]): You probably shouldn't do this -- it doesn't do what you probably think it does. You probably think that what happens is that if you call Modele(nom_fichier), the magasins attribute will be set to an empty list. But that's not what happens. Default values in Python are defined when the method is created, not when it is run. Watch this example: >>> class Test: ... def __init__(self, x=[]): ... self.x = x ... >>> a = Test() >>> a.x [] >>> b = Test() >>> b.x [] >>> >>> a.x.append("Surprise!") >>> b.x ['Surprise!'] How does this happen? Because every instance shares the same default list. When you append to it, all the other instances see the same change. You don't notice this with default values that are strings or numbers, because you can't modify them, only replace them: >>> x = y = 2 # Make two variables that point to the same value. >>> x is y # Make sure they are identical, not just equal. True >>> x = 3 # Make x point to something else. >>> x is y # And no longer identical. False >>> >>> x = y = [] # Make two variables that point to the same thing. >>> x is y True >>> x.append('something') # Modify that list in place. >>> x is y # Still identical. True >>> y ['something'] If this is the behaviour you want, then you don't need to do anything. Otherwise you need to move the creation of the empty list inside the method: def __init__(self, nom_fichier, magasins=None): if magasins is None: magasins = [] self.nom_fichier = nom_fichier self.magasins = magasins -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Hi everybody stuck on some error need help please thank you!!
Hi everybody, I would appreciate your help on this one In this program I want to create 2 concepts each with 2 or 3 properties My first concept is magasin(shop in french) and my shop has 3 attributes: nom(name in french), items and ville (city in french) the second one is items and its 2 attributes are nom(name in french) and prix (price in french) I want to be able to show a modele with the name of my magasins (stores) and the city theyre located in, what are the names of the items i have in each magasin and their prices. Here's my code: class Magasin: """ Le concept magasin pour la gestion d'inventaire des items de golf. """ def __init__(self, nom ="", items =[], ville="" ): self.nom = nom self.items = items self.vile = ville def set_nom(self, nom): self.nom = nom nom = property(None, set_nom) def set_items(self, items): self.items = items items = property(None, set_items) def set_ville(self, ville): self.ville = ville items = property(None, set_ville) def __str__(self): return self.nom class Item: """ Le concept item pour la gestion d'inventaire des items de golf. """ def __init__(self, nom ="", prix = 100): self.nom = nom self.prix = prix def set_nom(self, nom): self.nom = nom nom = property(None, set_nom) def set_prix(self, prix): self.prix = prix prix = property(None, set_prix) def __str__(self): return self.nom class Modele: """ La definition d'un modele avec les magasins. """ def __init__(self, nom_fichier, magasins =[]): self.nom_fichier = nom_fichier self.magasins = magasins def set_nom_fichier(self, nom_fichier): self.nom_fichier = nom_fichier nom_fichier = property(None, set_nom_fichier) def set_magasins(self, magasins): self.magasins = magasins magasins = property(None, set_magasins) def sauvegarder(self): modele_fichier = open(self.nom_fichier, 'w') for magasin in self.magasins: modele_fichier.write("===MAGASIN===" + "\n") modele_fichier.write("nom : " + magasin.nom + "\n") modele_fichier.write("items : " + "\n") for item in magasin.items: modele_fichier.write("---ITEM---" + "\n") modele_fichier.write("nom : " + item.nom + "\n") modele_fichier.write("prix : " + item.prix + "\n") modele_fichier.write("---FIN ITEM---" + "\n") modele_fichier.write("===FIN MAGASIN===" + "\n") modele_fichier.close() def charger(self): magasins = [] try: modele_fichier = open(self.nom_fichier, 'r') except IOError: print("Le fichier " + self.nom_fichier + " n'existe pas.") else: fin_fichier = False while not fin_fichier: ligne = modele_fichier.readline() if ligne == "": self.set_magasins(magasins) modele_fichier.close() fin_fichier = True break magasin = Magasin() items = [] fin_magasin = False while not fin_magasin: ligne = modele_fichier.readline().strip() if ligne.startswith("===FIN MAGASIN==="): magasin.set_items(items) magasins.append(magasin) fin_magasin = True elif ligne.startswith("nom"): nom = ligne.split(':')[1] magasin.set_nom(nom.strip()) elif ligne.startswith("---ITEM---"): item = Item() fin_item = False while not fin_item: ligne = modele_fichier.readline().strip() if ligne.startswith("nom"): nom = ligne.split(':')[1] item.set_nom(nom.strip()) elif ligne.startswith("prix"): prix = ligne.split(':')[1] item.set_prix(float()) elif ligne.startswith("---FIN ITEM---"): items.append(item) fin_item = True def vide(self): if self.magasins == []: return True else: return False def initialiser(self): magasin01 = Magasin ("Swing de golf") magasin02 = Magasin ("Golftown") magasin03 = Magasin ("PointGolf") item01 = Item ("Ensemble de fers Titleist") item01.set_prix("1099.99") item02 = Item ("Ensemble de fers Callaway") item02.set_prix("1299.99") item03