Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone ,
Please what is the difference between when we name an attribute by self.message and when we name it as message like an argument of a method like here :
Python
def talk(self,message)
    print({} sad : {}".format(self.name,message))

Python
def talk(self,message)
    print({} sad : {}".format(self.name,self.message))

and thank you .

What I have tried:

coding on the text editor searching on the net
Posted
Updated 30-Aug-21 4:23am
v2
Comments
Richard MacCutchan 30-Aug-21 12:51pm    
In case 2 the parameter "message" passed to the talk function is ignored.

1 solution

If your code was complete - which it isn't, it's missing a close bracket, then ...

"self.something" refers to a field, method or property of the current class instance called "something", in the same way that "my car glove box" would reference to the specific glove box on your car, and have no interest in the content of the glove box on that car over there, you know - the grey one.

So if you put your mobile in the glovebox of your car and then come out in my car with me, you don't expect to find you mobile in my car's glove box - my car is a difference instance of the class of "cars" from your car.

In Python, self refers to the specific instance you are working with at the moment, and self.gloveBox refers to the current car's glove box.

When you provide a parameter of the same name though, you access that without self because you are talking about the specific value that the method was called with rather than a property of any specific class instance.

See here: What is the use of self in Python? self in Python Class Example | Edureka[^]
 
Share this answer
 
Comments
Mohamed AIT HADDOU 31-Aug-21 2:02am    
thank you so much

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