Deprecate self

Rainer Deyke root at rainerdeyke.com
Wed Apr 18 10:40:41 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> wrote in message
news:9bjl2901ifj at news1.newsguy.com...
> "Dave LeBlanc" <whisper at oz.net> wrote in message
> news:9bji5k$417$0 at 216.39.170.247...
> > Since self is used for every method of every class, isn't it a bit
> > redundant?
>
> Not really.  Explicit is better than implicit.

The thing that bothers me about using 'self' is that it is inconsistent.
The relationship of a method to the object on which it is called is
analogous to the relationship between a free function and its module, yet
only the former supports the 'self.x' convention.  I could do this:

# Module spam.py
import spam
x = 5

def f():
  print spam.x

However, this is still inconsistent because the name 'spam' is already
accessed through the global namespace.  I could do this:

# Module eggs.py
import eggs
x = 5

def f(self_module=eggs):
  print self_module.x

However, this does not work if 'f' takes a variable number of argument (and
is tedious besides).  In C++, I can explicitly qualify global variables:

namespace jam {
  int x = 5;
  void f()
  {
    std::cout << ::jam::x << std::endl;
  }
}

Another thing that C++ got right and Python got wrong.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list