PEP: XXX Title: Shorthand Symbol for "self" Version: $Revision$ Last-Modified: $Date$ Author: Russ Paielli Status: Draft Type: Standards Track Content-Type: text/plain Created: 03-Aug-2008 Python-Version: 3.1 Post-History: Abstract In Python, the class instance for which a method is called is referred to within the method by the first formal argument. By convention, that first argument is typically named "self". Attributes of "self" are referred to using the standard "dot" notation, as in "self.attr". As a result, complex methods can become cluttered with many occurrences of "self.". This PEP proposes to allow the dollar symbol, "$", to be used as a shorthand symbol for "self." or, in general, for ".", where "" is the name of the first argument. Thus, "self.attr" could be written more succinctly as "$attr". This convention actually makes the code slightly easier to read, because the person reading it need not remember the name of the first argument nor refer back to it. The proposal applies to class methods as well, except that the first argument then refers to the class itself rather than an instance of the class. Rationale In Python, class instance methods are functions in which the first formal argument refers to the instance for which the method was called. The first argument can be any valid identifier, but the common convention is to call it "self". Within a method, an attribute "attr" of the instance is then referred to as "self.attr". Invokations of other methods of the same class for "self" are also prepended with the "self." qualifier. In complex methods, the repeated occurrence of "self." tends to clutter the code. The clutter can be reduced by replacing the name "self" with a shorter name (e.g., "s"), but that is widely considered bad practice for production code. Another option is to explicitly "strip off" the "self." by writing "attr = self.attr" to create a local reference to the attribute within the method. While that approach allows complicated algorithms to be expressed more succinctly and elegantly, it adds extra lines of code, and it also burdens a person reading the code with keeping track of which data is local and which belongs to the class. Java, C++, and other object-oriented languages do not require an explicit "self." qualifier within methods. Those statically typed languages can allow that simplification, because all attributes and methods of a class must be declared in the class declaration. In Python, attributes can be added in methods or even outside the class definition itself. Hence, eliminating the "self." qualifier is neither desirable nor feasible. Although the explicit "self." qualifier cannot be eliminated in Python, a simple syntactic convention can be used to reduce code clutter. The proposed convention is to use a special character as a shorthand symbol for "self." or, more generally, for ".", where is the first argument of the method. Four possible candidates for the choice of that symbol are "~", "@", "^", and "$". The tilde, "~", resembles a negative sign, so it is not a good choice. The "at" symbol, "@", is used for function decorators, so it is not the best choice. The carat symbol, "^" is a possible choice, but the dollar symbol is already widely prepended to variable names in Perl as well as bash and many other shell scripting languages, albeit for a different reason than proposed here. The recommendation here is therefore to let the dollar symbol be optionally used as a shorthand for "self." or, in general, for ".", where is the name of the first formal argument of the method. This would allow "self.attr" to be written more succinctly as simply "$attr". (For simplicity, whitespace should perhaps not be allowed between "$" and the attribute or method name, but that is not required as part of this proposal.) In addition to reducing code clutter, another advantage of this proposal is that a person reading the code need not remember the first argument nor refer back to it. The dollar symbol would have the same meaning regardless of the name of the first argument. This proposal, therefore, actually improves readability slightly. This proposal could be easily implemented as a simple one-pass pre-processor. Although such a pre-processor is not recommended for the ultimate implementation, it could be used to implement this proposal independently if it is not accepted as a standard. Such an implementation would constitute a "fork" of Python, but it would be a fork that could never diverge from the Python standard (unless the dollar symbol is given some new, unrelated meaning in a future version of Python, which seems unlikely). This proposal works for class methods as well as class instance methods. The only difference is that, for class methods, the first formal argument refers to the class itself rather than an instance of the class. Otherwise, everything works the same. It could even work for regular functions if the first argument is an object with attributes and/or methods that can be accessed with standard "dot" notation. This proposal takes no position of whether the latter should be allowed or not. Potential Enhancements If the dollar symbol is used in place of "self" everywhere in a particular method, then the first formal argument becomes an arbitrary placeholder. A possible syntactic enhancement in that case could be to allow the first argument itself to be a dollar symbol. An additional possible syntactic enhancement could be to allow the dollar symbol to refer to "self" when used by itself (rather than prepended to another identifier) within the method. However, these potential enhancements are separate from the main proposal and can be rejected without rejecting the main proposal. References None Copyright This document has been placed in the public domain. Local Variables: mode: indented-text indent-tabs-mode: nil sentence-end-double-space: t fill-column: 70 coding: utf-8 End: