self

Andrew Koenig ark at research.att.com
Tue Jun 4 12:52:24 EDT 2002


Vojin> I have been working on a project trying to implement some
Vojin> advanced functionality.  Now, one thing that has been a
Vojin> constraint for me is the concept of self.  Why is it that
Vojin> within a class one has to use self. (or "any word". ) to refer
Vojin> to instance variables? Would it not be more logical to just use
Vojin> the name of the variable like it is for example in C++?

It has advantages and disadvantages.  The obvious disadvantage of
having to use "self" to refer to every instance variable is that
you have to use "self" to refer to every instance variable.

The advantage of having to use "self" to refer to every instance
variable is that there is never any doubt about whether a given name
represents an instance variable.  These doubts can cause trouble for
C++ programmers; whether the convenience of not having to utter
"this->" constantly is a matter of personal taste and opinion.

For Python, requiring the explicit use of "self" is clearly the
right choice because variables are so dynamic that it would not
generally be possible to tell during compilation whether a
particular name refers to an instance variable.

-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark



More information about the Python-list mailing list