Using attributes in python

Hello everyone ,
Please what is the difference between when naming an attribute by self.message and when naming it by message as an argument of a method like here:

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

second method

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

and thank you

They are completely different things:

  • self.message reads an attribute of the self object (or tries to, it will fail if the object doesn’t have such an attribute).

  • If you’re passing a message parameter to a function and reference that, whatever has been passed in is used.

Which makes sense to do depends on what you’re actually doing. Using self sounds like you’re learning about objects. :slightly_smiling_face:

Please can you more explain i didn’t understand yet

If you have more questions, please ask them specifically. Or check the Python tutorial chapter on classes and objects, it’s pretty extensive: 9. Classes — Python 3.9.7 documentation