2

When I first learned python I thought the first parameter of any method must be 'self'. Now that i'm more experienced with python, I wanted to see if that was actually the case. For example:

class a:
   def __init__(this):
       this.test = "hello"

At first when I wrote this I thought I was insane when it actually worked perfectly fine, but now I am very curious as to why it is always taught to use self instead of other words instead. Please note that I am NOT asking why my example works, I understand completely why it works, I am asking why we always learn to use self, and in a lot of python courses it is taught that using self is required, when you can actually just use any word instead.

Bale
  • 553
  • 1
  • 8
  • 19
  • 4
    Interesting. I always use `me` because (1) I don't want my Python buddies to know I have a relationship on the side with C++ (by using `this`); and (2) I like annoying the hell out of my Python buddies :-) But, if you're doing courses where they say it's required, reconsider the intelligence of those giving the courses. It's good to be consistent (e.g., PEP8) but it's by no means a requirement. – paxdiablo Sep 08 '20 at 02:11
  • This isn't insane at all. This is banal. There is nothing special about the name `self`, it is *merely a convention*. Now, you *should* follow conventions if you want to write clear code, but the relevant part is that it is *the first argument to a method which gets bound to the instance*, regardless of the name. – juanpa.arrivillaga Sep 08 '20 at 02:17
  • @paxdiablo Could you post this as an answer, maybe with some extra details and explanations. – Tarik Sep 08 '20 at 02:17
  • It is indeed just a convention. I assume it is because Master Guido (Guido van Rossum, the creator of Python) chose it. He's very opinionated, and so in this case, I expect that's the one and only reason. – CryptoFool Sep 08 '20 at 02:17
  • `self` is the convention. Like in many other features of Python the developer is not limited here but is expected to make the reasonable choice to follow the convention. – Klaus D. Sep 08 '20 at 02:17
  • 1
    Yeah, why not change your name daily? You'd still be yourself, you as a person would not change. I am sure you can imagine a few practical consequences though – Pynchia Sep 08 '20 at 02:18
  • 1
    If we are going to change it, let's change it to "shrubbery"! :) – CryptoFool Sep 08 '20 at 02:19
  • 2
    Does this answer your question? [Naming the self parameter something else](https://stackoverflow.com/questions/37885330/naming-the-self-parameter-something-else) – wjandrea Sep 08 '20 at 02:20
  • 4
    I hate that it says I voted to close the question as "opinion-based". I definitely didn't, I voted to close it as a duplicate. – wjandrea Sep 08 '20 at 02:21
  • I voted to re-open, I'll close as duplicate once it does – juanpa.arrivillaga Sep 08 '20 at 02:25
  • @wjandrea: it does *not* say you closed it as opinion-based, it says it was closed as opinion-based. It then says you were one of the ones who closed it. There is no connection in the explanatory text between you and the reason chosen. And, to juanpa, voting to re-open just so you can close it again is bizarre. You *do* realise that ate least one of those people *did* vote as opinion-based, and that you'll just shift the "incorrectness" from one person to another :-) – paxdiablo Sep 08 '20 at 02:31
  • In any case, the question *is* opinion-based. I'm pretty certain the rather large number of Python developers across the world won't all share the one single answer to this question. Example, I use it because of convention, others use it because they're unaware they can do something else. – paxdiablo Sep 08 '20 at 02:35
  • @juanpa reopened. i can't VTC again cause I already did – wjandrea Sep 08 '20 at 03:17
  • @paxdiablo Well it doesn't say it explicitly, but it implies it. Anyway, nbd – wjandrea Sep 08 '20 at 03:17
  • Well, it is just convention. Just like in mathematics, we use `f(x)` to denote functions, `x` to denote variables, `c` as constants, `ABC` to denote triangle, etc. Using `self` is just convention and you shouldn't change it unless for obvious reason. – wong.lok.yin Sep 08 '20 at 03:51
  • Brilliant! So, because some people didn't like the *reason* why a question was closed (though they agreed it should *be* closed), it's now open. Way to go, guys, stellar job :-) – paxdiablo Sep 08 '20 at 05:32

0 Answers0