I've been playing around with Godot for a couple of years now on various hobby projects, and coming from a software engineering background with python experience, working with gdscript has been great. I'm always on board with making code as clear as possible, even if it means extra typing, so I'm a big fan of type hints and have noticed a lot of documentation/tutorials use them as well.

One pattern that I don't see anyone else using is invoking "self" to be explicit when referencing an instance variable or method. For example, rather than: _instance_variable = _method() I would tend to write: self._instance_variable = self._method()

I find it very helpful to clearly differentiate between local variables (which go out of scope as soon as the method returns) and instance variables (which persist as part of the object's state), and seeing at a glance that I'm calling a method (which only exists in some classes) rather than a built-in function (which will work everywhere) is nice too. I don't see anyone else doing this though, even people who use type hints and other careful formatting patterns.

So what I'm wondering is, is this just a matter of convention and personal preference, or is there actually an argument against using "self" that I should take into consideration? (It's never caused me any trouble but I also have been working on pretty small scale games so far.)

I love using self to remind myself which variables are part of the script/class. I see others do it from time to time but not often. Gdscript is one of those "loosy goosy" (computer science lingo) languages where you can be as specific as you want (almost) but don't have to. I don't ever use self.functionName() however.

If you're working with a team then I'd say adhere to a convention. I don't think there's any penalty whatsoever to using self. I throw it around everywhere in my larger projects and have never had a problem.

I just reviewed some of my GDScript code.

There are very few local variables. Almost every variable would require "self", if I used that convention. In my opinion, that would make the code less readable.

And almost all of my functions are short. It's easy to see the local variable declarations if I need to know whether a variable is local.

It's implied. Most of the time you are calling functions in the class, which shouldn't need self and are more readable with just the function name. And for local variables, they would be defined in the function scope, so will have var whatever right there, letting you know they are local. I don't feel there is any good reason to use self, though of course it is a valid choice if that makes your code clearer to you.

a year later