Why self?

Bjorn Pettersen BPettersen at NAREX.com
Tue Jul 9 15:20:07 EDT 2002


> From: Charles Hixson [mailto:charleshixsn at earthlink.net] 
> 
> If you are subclassing a class that someone else has written, 
> you don't 
> want to accidentally redefine an internal name.  Sorry.  I can accept 
> that you should be able to explicitly access it, but to be able to 
> change it by accident is (would be?) a misfeature.
> 
> I didn't believe that this happened in Python.  It does, and 
> it seems a 
> significant flaw.  My understanding is that if you define a 
> variable in 
> a class that subclasses another class, then unless you take definite 
> steps to cause things to happen otherwise, you are defining a new 
> variable that is local to the scope of the class that you are 
> currently 
> defining, and will not impact routines that you call from parental 
> classes.  Experiment shows that this isn't what happens.
> from __future__ import nested_scopes
> does not fix this problem, as I assumed that it would.
> 
> Let's be explict:
> from __future__ import nested_scopes
> class A:
>     def ma(self):
>         print  "ma, ma"
>     def pa(self):
>         self.ma()
> class B(A):
>     def ma():
>         print "hello, world"
> tst = B()
> tst.pa()
> 
[snip]

You may have a point, but your example doesn't show it. It is generally
seen as one of the bedrocks of OO programming that you should be able to
redefine a method in a subclass for more specific behavior. In C++ this
is spelled virtual.

Whether the same is true of attributes is something I'll let other
people worry about...

-- bjorn





More information about the Python-list mailing list