Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of having this/self pointer mandatory explicit?

Tags:

What is the advantage of having this/self/me pointer mandatory explicit?

According to OOP theory a method is supposed to operate mainly (only?) on member variables and method's arguments. Following this, it should be easier to refer to member variables than to external (from the object's side of view) variables... Explicit this makes it more verbose thus harder to refer to member variables than to external ones. This seems counter intuitive to me.

like image 290
Piotr Dobrogost Avatar asked May 26 '09 10:05

Piotr Dobrogost


People also ask

What is the purpose of the self keyword when defining and calling methods?

What is the purpose of the "self" keyword when defining or calling instance methods? self means that no other arguments are required to be passed into the method.

Why do you have to pass self in Python?

The self is used to represent the instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments. The reason why we use self is that Python does not use the '@' syntax to refer to instance attributes.

Is self mandatory in Python?

Self is a convention and not a Python keyword . self is parameter in Instance Method and user can use another parameter name in place of it. But it is advisable to use self because it increases the readability of code, and it is also a good programming practice.

What is self variable in Python?

The self variable is used to represent the instance of the class which is often used in object-oriented programming. It works as a reference to the object. Python uses the self parameter to refer to instance attributes and methods of the class.


2 Answers

In addition to member variables and method parameters you also have local variables. One of the most important things about the object is its internal state. Explicit member variable dereferencing makes it very clear where you are referencing that state and where you are modifying that state.

For instance, if you have code like:

someMethod(some, parameters) {     ... a segment of code     foo = 42;     ... another segment of code } 

when quickly browsing through it, you have to have a mental model of the variables defined in the preceding segment to know if it's just a temporary variable or does it mutate the objects state. Whereas this.foo = 42 makes it obvious that the objects state is mutated. And if explicit dereferencing is exclusively used, you can be sure that the variable is temporary in the opposite case.

Shorter, well factored methods make it a bit less important, but still, long term understandability trumps a little convenience while writing the code.

like image 183
Ants Aasma Avatar answered Sep 22 '22 03:09

Ants Aasma


If you are talking about "explicit self" in the sense of Python, here's an interesting discussion on the topic. Here's Guido responding to Bruce Eckel's article. The comments on Bruce's article are also enlightening (some of them, anyway).

like image 44
Hank Gay Avatar answered Sep 21 '22 03:09

Hank Gay