[Python-ideas] PEP: Shorthand Symbol for "self"

Terry Reedy tjreedy at udel.edu
Mon Aug 25 03:26:49 CEST 2008



Jim Jewett wrote:
> On Sun, Aug 24, 2008 at 6:43 PM, Russ Paielli <russ.paielli at gmail.com> wrote:
>> ... I fully understand the history
>> and controversy regarding the explicit use of "self" in Python.
> 
> One of the problems with the current syntax is that calling sites
> don't match the definition site the way they do for normal functions.
> This proposal doesn't seem to help with that.
> 
> Normal function --
> 
>     def fn(a, b, z=None): ...
> 
>     fn(a, b, z)
>     fn(a, b)
> 
> Method --

Methods are normal functions accessed through a class attribute binding.
In 3.0, the unbound method wrapper that might have obscured this is gone.

>     def m(self, a, b, z=None): ...
> 
>     # self is moved outside the parens, changing the tuple size
>     self.m(a, b, z)
>     self.m(a, b)

If m is an attribute of type(s) (which is s.__class__), this shrinkage 
is a convenient abbreviation, not a requirement.  The above calls are 
the same as
type(s).m(s.a,b,z) or type(s).m(s,a,b).  Or, if you prefer,
fn = type(s).m # or s.__class__.m
fn(s,a,b,z)
If m is an attribute s, and s in not a class, the shrinkage is not 
available, and one must write s.m(s,a,b,z) or s.m(s.a,b).

Terry Jan Reedy




More information about the Python-ideas mailing list