Why self?

Brian McErlean b_mcerlean at yahoo.com
Sat Jul 6 10:23:45 EDT 2002


"Matt Gerrans" <mgerrans at mindspring.com> wrote in message news:<ag5kg6$e8l$1 at slb4.atl.mindspring.net>...
> 
> In Python, I think the required "self" reference in methods is a case where
> the solution is worse than the problem it solves.   Since indentation is the
> method of scoping in Python, this addition of five characters to each
> instance variable is particularly annoying -- a one-line statement that
> contains several instance variables can easily become unmanageable.
> 
> It's a bit like that horrible blight on the programming community called
> "Hungarian Notation" that needlessly polutes code with incomprehensible
> variable prefixes so that you know your string is really a long pointer to a
> zero terminated array of signed 8-bit characters.   As if you cared, or
> needed to be constantly innundated with such superfluous information.
> Fortunately, the Hungarian Notation investation has for the most part been
> limited to Microsoft's C and C++ APIs and thier usage, but hasn't infected
> others -- and nowadays it even seems to be on the decline in Redmond (C#
> examples are generally free of it).

I don't entirely agree.  The thing that really irritates me about
hungarian notation is that you're mangling the human readable names of
things to provide information that is related to the internals of the
language.  The name of a variable should tell me what its for, not
what or where it is.  Conventions like "m_" are in some ways worse -
now you're abusing the names to resolve language namespace issues.  On
the other hand, I think that "this->" and "self." are the right way. 
You are using the language to say what you mean, not mangling names to
get the same effect.

> I never had a much of problem in C++ with distinguishing member variables
> from local variables and I never liked those cheesy ("Some cheese, please,
> my good man!") conventions of always using "m_" or "this->".   If you keep
> your methods small and clean, you don't need all this slop.
> 
> However, since Python doesn't have declarations, some method is required to
> distinguish whether an assignment in a class's method is an assignment to a
> instance variable or a local variable.   I prefer the solution of simply
> making it illegal to have local variable names match class variable names.
> Hmm... this doesn't help with the problem of globals, but there is a keyword
> for that.

This leads to the same problem.  Its pretty common for constructors to
take arguments that correspond directly with instance variables. eg.

class AddressBookEntry:
  def __init__(self, name, address):
    self.name = name
    self.address = address

Under your situation, I can't just omit self, since then there would
be instance and local variables with the same name.  The only thing I
can do is to mangle either my instance variables or my arguments.  I'm
back to having to append "_arg", or "_i" to my variables.

In any case, in a language like python, what you suggest may be
infeasible anyway.  Methods can add instance variables at will, so
assignment at least would have to use self. to distinguish between
using a new local variable.

Brian.



More information about the Python-list mailing list