I'm finding that my code becomes more readable when I make a local reference to this/self, and/or call it something that makes sense in the context. "self" or "this" doesn't really indicate what kind of object we are dealing with, in the same way as "default" or "standard" or "master" is unclear because it's not explicit.

For example, in Python code, instead of naming the instance reference "self", you could name it something else:

class User:
    def getFullName(user):
        return user.firstName + " " + user.lastName

What are the arguments for naming it self, instead of something more explicit, like "user" in this case?

A similar example in JavaScript when doing a manual mixin:

function asUser(obj, name) {
    var user = obj;
    user.name = name;
    return user;

I guess in JavaScript this is less controversial because there is not strong tradition of using a certain naming, as in the case of "self" in Python.

  • 4
    In Python: self is such an established convention, that I find using anything else extremely surprising. And what if the method interacts with other users? self is crystal clear, you are working on the current instance, the one the method is bound to. – Martijn Pieters Nov 19 '14 at 15:36
  • 7
    Going against convention reduces the amount of information your code conveys even if the variable name is arguably more explicit. A Python programmer sees self as the first argument of a method and knows exactly what that means. If they see anything other than self they'll waste time trying to figure out 1) are you doing something different? 2) apparently not, so why did you break the convention? – Doval Nov 19 '14 at 15:36
  • 2
    Good points and I agree to some extent. But I also feel that.. do you really need to know that you are working on the current instance? You have code that operates on a user object. Does it really matter if that user object is the "instance" or some other user object? – Magnus Wolffelt Nov 20 '14 at 16:31
  • 1
    If it doesn't matter whether you are working with an instance or some other object, why not through out OO entirely and just change user.getFullName() calls to getFullName(user)? – Gort the Robot Dec 28 '14 at 21:20
  • @MagnusWolffelt I think the Python and JS cases are not the same. In Python you name the first parameter of a method self to indicate that it's the object that got passed in from invoking object.function(). In JS I would much rather write var user = this than var that = this or some nonsense. In the JS case there is no agreed-upon convention anyway (that, self, _this, etc.). And the point of aliasing this is to have a normal lexically scoped variable that doesn't disappear inside a closure (so why not name it like one?). We don't have that problem in Python. – Hey Dec 29 '14 at 18:39

In general I feel it might make your code less readable. Simply because inside different classes you're going to use different words to reference the instance. I refactor a lot of code for a living and a this, self or even _this is immediately scanned by me as the reference to the instance.

This may not exactly fit your requirements but in JavaScript we do have to frequently rename this! The this keyword loses context inside callbacks and refers to window / global!

https://stackoverflow.com/questions/337878/var-self-this?lq=1 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

var _this = this;
var self = this;

Is how most JS libraries roll!


This is a huge mistake. self is a well-worn convention in Python and so any Python programmer who encounters this code is going to immediately be confused. Any experienced programmer will see self and will instinctively think "I am looking at a method definition and this particular line is accessing an attribute of the object".

Name it something else, and you've lost the advantage of that instinct. For a two line method as above, this will produce a few minutes head scratching. If the method is a complex one, maybe twenty lines, it will produce a lot more confusion, and the average python programmer will repeatedly end up thinking "oh wait...user is the self object." That will waste time if nothing else.

Confusion will multiply if any code outside the object uses user as a name, which is, of course, the obvious thing to name an object of type User outside the class. This means you will continually be thinking "Oh wait, am I inside the class or not?"

Speaking as a long term C++ and then Python coder, your first example just seems weird. My brain does not even want to think of it as a method despite the fact that it knows it is. user looks like something being "passed in" from outside the object, and it is actual mental work to realize that it is not something external to the object.

Convention is massively important in code readability. It's why we have style guides. One thing that earns python's reputation for readability is that it has a clearly defined style guide that most conform to. self is a part of that.

  • 2
    Not to mention that any editor worth its salt will highlight self but not user. – Benjamin Hodgson Dec 29 '14 at 13:56
  • I'm not sure if I agree that it's a huge mistake. And is it really that important to know whether you are "inside the class"? I could argue that it is mental work to map self to the containing class name. A well chosen self-name reads more naturally than self, in my opinion. – Magnus Wolffelt Jan 4 '16 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.