I don't like the OO part of python. In particular the "self" keyword everywhere.

Michael Torrie torriem at gmail.com
Thu Jun 11 10:15:19 EDT 2015


On 06/11/2015 05:19 AM, Skybuck Flying wrote:
> I haven't written much OO code yet in Python... and don't plan on doing it 
> too...

Except that you already have written OO code in Python with your parser.
 Or at least code that interacts heavily with OO.  Anytime you call a
method on a string like split(), or worked with regular expressions, you
are using OO.  OO permeates Python.  You don't have to use OO design in
your programs, but you will always be working with objects and calling
methods on them.

But anyway, if you don't like "self," use a different name.  Like "skybuck."

class Bar:
    def foo(skybuck, a, b):
        skybuck.a = a
        skybuck.b = b

In other languages with an implicit self or this like C, I find the
ambiguities of scope highly problematic.  Method variables can shadow
instance variables.  It's also difficult for someone unfamiliar with the
code to differentiate between a local variable and an instance variable
as the naked variable name has no qualifier.  I've seen some projects
that apply a "m_" prefix to every instance variable so you can tell them
apart.  Sure that's less typing, but it is super ugly!  Explicitly
providing self not only makes things very clear, it also helps me when
I'm scrolling through code to identify which functions definitions are
bare functions and which are methods of a class.

In short, it's part of the Python language and will not be changing
anytime soon.  So no the interpreter couldn't be written in such a way
that the self can be left out without changing the language's semantics
and definition.




More information about the Python-list mailing list