Click here to Skip to main content
15,851,701 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Bonjour tout le monde , s'il vous plaît ici le paramètre "Mohamed" est lié au attribut du constructeur comment on l'a fait passé à la méthode parler, comment ça fonctionne au niveau du python ?
Python
class Humain:
     """ Classe qui définit un humain """


     def __init__(self, nom, age):
         self.nom = nom
         self.age = age
        
         

     def parler(self, message): 
         print("{} a dit : {}".format(self.nom,message))

        
       
#Programme principal
h1 = Humain("Mohamed", 17)

h1.parler("Bonjour tout le monde")


What I have tried:

Rechercher du sujet sur internet
Posted
Updated 15-Aug-21 21:33pm
v2
Comments
Richard MacCutchan 16-Aug-21 3:54am    
See my answer to your other question. I am not sure how you could search the internet and not find the Python tutorials.

1 solution

Firstly, this is an English language site, and we can only accept and answer questions in that language.

Premièrement, il s'agit d'un site en anglais, et nous ne pouvons accepter et répondre aux questions que dans cette langue.

Secondly, You have a class constructor that takes three parameters, one of them "virtual" in that you don't have to supply it when you call the constructor as the system will do that for you, and two "real" that contain information teh class needs in order to create a new instance:
Python
def __init__(self, nom, age):
    self.nom = nom
    self.age = age
self is the "virtual" parameter which is the instance of the class you are creating.
nom is the name of the person you are working with in the class instance.
age is their age.

When you call it, you provide the instance specific values:
Python
h1 = Humain("Mohamed", 17)

And the system uses them to fill in the parameter variables inside the constructor, which you then use to store them as a part of the class.
Python
self.nom = nom
self.age = age
When you later call your function parler on the class instance, it accesses the class based variables you stored in the constructor and displays that for you.
 
Share this answer
 
Comments
Mohamed AIT HADDOU 16-Aug-21 3:01am    
thank you very much
OriginalGriff 16-Aug-21 3:28am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900