[Tutor] Hi everybody stuck on some error need help please thank you!!

Marco Rompré marcodrompre at gmail.com
Sat Apr 24 05:33:46 CEST 2010


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 <steve at pearwood.info>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 <module>
> >     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 at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Marc-O. Rompré
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100423/3fc6fb1f/attachment.html>


More information about the Tutor mailing list