why is "self" used in OO-Python?

Fredrik Lundh fredrik at pythonware.com
Tue Jul 15 13:10:15 CEST 2008

ssecorp wrote:

>     def append(self, item):
>         self.stack.append(item)
> I can get to see the stack with var.stack but then why even implement
> append when I could do self.stack.append(x) etc.
> That way you could do away with OO completely.

Umm.  Even if you were to write that, self and stack would still be 
objects, and the "append" would still be a method defined by the stack 
object, right?

What you seem to be referring to is the Law of Demeter, which is a 
design guideline for avoiding unnecessary coupling, not an absolute 
requirement for object-orientation:


As for the rest, I suspect you will have more success in using Python if 
you use it to write Python programs, not Java programs:



More information about the Python-list mailing list