History of 'self' and why not at least 'my'?

Giorgi Lekishvili gleki at gol.ge
Fri Jan 11 15:38:35 EST 2002


I would like rather to have something akin of Pascal's or VB's 'with', e.g.,
with self:
    bla1,
    bla2
    ...

Grtz,
Giorgi

Fernando Pérez wrote:

> I'm curious as to why 'self' was chosen instead of the shorter 'my' for
> naming the instance passed to methods.
>
> def meth(my,x,y,z):
>   ...
>   my.x=1
>   my.y=2
>
> is IMHO even more naturally readable than
>
> def meth(self,x,y,z):
>   ...
>   self.x=1
>   self.y=1
>
> and saves typing two characters every time (not negligible considering how
> many times you type self!).
>
> In my code I respect the 'self' convention simply because I don't like
> breaking well-accepted conventions unless there's a tryly compelling reason
> to do so. But lately I've ended up using the following little function a lot:
>
> def setattr_list(obj,alist,nspace):
>     """Set a list of attributes for an object taken from a namespace.
>
>     setattr_list(obj,alist,nspace) -> sets in obj all the attributes listed in
>     alist with their values taken from nspace, which must be a dict (something
>     like locals() will often do).
>
>     Note that alist can be given as a string, which will be automatically
>     split into a list on whitespace."""
>
>     if type(alist) is types.StringType:
>         alist = alist.split()
>     for attr in alist:
>         val = eval(attr,nspace)
>         setattr(obj,attr,val)
>
> in the following manner:
>
> def meth(self,...):
>   x=1
>   y=2
>   ...
>
>   setattr_list(self,'x y z....',locals())
>
> because I hate the extra typing. I think with a shorter, leaner 'my' I
> wouldn't do this and the code would be overall clearer.
>
> Any comments on the history of self's choice?
>
> And on using shorter conventions like 'my'? (I won't go as far as using
> simply 's', single letter names should only be used for simple counters IMO).
>
> Anyway, just curious before going to bed :)
>
> Cheers,
>
> f




More information about the Python-list mailing list