NameError: name 'self' is not defined


Bonjour, j'ai une erreur que je n'arrives pas à corriger, la console indique systématiquement ce message:

Traceback (most recent call last):
  File "python", line 13, in <module>
NameError: name 'self' is not defined

Et voici mon code

class Voiture(object):
    condition = "nouveau"
    def __init__(self, modele, couleur, mpg):
        self.modele = modele
        self.couleur = couleur
        self.mpg   = mpg
    def afficher_voiture(self):
        return "Ceci est une %s %s avec %s MPG." %(self.color,self.model,str(self.mpg))
ma_voiture = Voiture("DeLorean", "argent", 88)
print ma_voiture.afficher_voiture(self,modele,couleur,mpg)

Si vous pouvez m'aider merci bien !



Have you considered what the error message says? It's giving you a line number and is saying that the name self doesn't exist in that context. Do you think it should exist there? If so, when would it have been created? If you don't think it should exist, then perhaps you should use something else or nothing at all?


I start the same topic twice by mistake and receive an answer, and i write this:

print ma_voiture.afficher_voiture()

instead of this :

print ma_voiture.afficher_voiture(self,modele,couleur,mpg)

and it's works, i call the method without parameter.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.