-2

What is the difference between when we use a variable as an attribute like self.message and when we use it as a parameter like message in a method:

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

def talk(self, message)
    print("{} sad : {}".format(self.name, message))
luther
  • 5,195
  • 1
  • 14
  • 24
Mohamed
  • 19
  • 2
  • Does this answer your question? [What's the difference between an argument and a parameter?](https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter) – PCM Aug 31 '21 at 07:03
  • Sorry, I have no idea at all what you're trying to say. Also, your example code contains numerous typos. – Karl Knechtel Aug 31 '21 at 07:09
  • I have seen it ,But i didn't find what i search – Mohamed Aug 31 '21 at 07:09
  • 2
    In general, "what's the difference" questions are not very useful. Usually, the two things don't actually have anything to do with each other, and so there are really two questions - "please explain the first thing" and "please explain the second thing". On Stack Overflow, you are only supposed to ask one question at a time, and we cannot just "explain" something to you because there is no way to know what needs explaining - see https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question . – Karl Knechtel Aug 31 '21 at 07:10
  • ```self``` parameter is added to make the function an attribute of the class. –  Aug 31 '21 at 07:10
  • @KarlKnechtel: I have to completely disagree. Asking "What's the difference" is an excellent way of focusing the question and telling us exactly what aspect of the concepts the questioner doesn't understand. Asking "Please explain this thing", on the other hand, is completely open-ended and tells us nothing about what part they don't understand. Aside from the typos (which I fixed) this question is perfectly clear. – luther Aug 31 '21 at 07:59
  • Let's see your answer to it, then. – Karl Knechtel Aug 31 '21 at 20:15
  • @KarlKnechtel: The question is currently closed, and is therefore not allowing answers. I would like to write an answer when it's reopened. – luther Aug 31 '21 at 20:30
  • 2
    I voted to reopen, because I'd like to see you try to prove me wrong :) – Karl Knechtel Aug 31 '21 at 20:33

1 Answers1

0

A parameter is really just a variable. It is declared by the function signature, and it gets assigned one of the arguments as part of the function call process.

An attribute is not a variable at all. It is a part of an object, similar to an index inside []. It is accessed using dot notation.

In your code, both functions declare a message parameter, but only the second one uses it. The first function gets all its information from the self object instead of using its message parameter.

luther
  • 5,195
  • 1
  • 14
  • 24