Why self?

Louis M. Pecora pecora at anvil.nrl.navy.mil
Tue Jul 9 13:59:06 EDT 2002


If I may ask some questions and make a few statements:

In article <yu9965zpnjij.fsf at europa.research.att.com>, Andrew Koenig
<ark at research.att.com> wrote:

> 1) In Python, it is generally not possible to determine what the
>    attributes of an object are by statically examining the source.
>    For example:
> 
>         class Thing:
>                 def __init__(self):
>                         setname(self)
> 
>                 def printname(self):
>                         print self.name
> 
>         def setname(x):
>                 x.name = "Thing"
> 
> At the time the compiler encounters the "print self.name" statement,
> how would it know that name is an attribute of the object of which
> printname is a member?  Note that it hasn't seen the definition of
> setname yet.

This one is lost on me.  Sorry, I'm not operating at your level.  What
is the point?  I advocated initializing object variables in a mandatory
__init__ statment.  So you should have added 

       def __init__(self):
           name=None

or something like that to be consistent with what I suggested (which by
the way was a stab from the top of my head right at that moment).  Now
the point was made that this might cause excess overhead, slowing down
the execution.  That I cannot comment on, but explicitly naming object
variables in __init__ or wherever might be best seems to take care of
this example nicely (execution speed aside) and makes the object
variables _explicit_.  Did I miss something?


> 2) Methods are just functions that happen to be attributes.
> In other words, we could have written this:
> 
>         class Thing:
>                 def __init__(self):
>                         setname(self)
> 
>         def setname(x):
>                 x.name = "Thing"
> 
>         def printname(self):
>                 print self.name
> 
>         Thing.printname = printname
> 
>         t = Thing()
>         t.printname()
> 
> effectively transforming printname into a method, even though it
> does not have the form of a method when it is written.
> 
> It seems to me that dropping the explicit "self" would make these
> two programs much messier.  In what way then is it cleaner?


Hmmm...a pretty bizzare example or else I just don't have enough
programming experience.  I've never done anything close to that.  I
will have to rely on others experience (including yours, which I know
is substantial).  Is this realistic or are you just pulling out a
pathological example for argument?  After all, I don't think it's the
duty of a language design to completely protect the programmer from
foolish code (for want of a better phrase).  At some point the language
has to just give up trying to interpret things and "raise and
exception."

Tell you what. You're a smart guy (not sarcastic, I mean it).  Why not
apply your considerable knowledge to finding a solution to the issue of
requiring "self."  prefixes?  It's apparent that lots of people would
like to lose that requirement.  I'm sure you can demolish my
off-the-cuff suggestion at some point.  I'm a bush-league amateur. But
when you do that you will turn around to see that the issue itself is
still there.  What would you do?

-- 
Lou Pecora
  - My views are my own.



More information about the Python-list mailing list