Another try at Python's selfishness

n.estner at gmx.de n.estner at gmx.de
Thu Feb 2 17:08:22 EST 2006


Having read previous discussions on python-dev I think I'm not the only
Python programmer who doesn't particularly like python's "self"
parameter:

    class Foo:
        def bar(self, a,b):
            return a+b
    Foo().bar(1,2) => 3

The main reason (at least for me) is that there's simply too much
"magic" in it. Why does the expression left of the '.' get promoted to
the first parameter? It even goes further:

    Foo.bar(Foo(), 1,2)

works, but:

    Foo.bar(1,2,3)

doesn't, just because of the "magical first parameter" in a member
function. But:

    Foo.__dict["bar"]__(1,2,3)

Does work.

The point is, I _do_ think it's a good idea to explicitly write
"self.SomeMember" for member-access, so I thought: why can't we be
equally explicit about member function declaration? Wouldn't it be nice
if I could write (say, in Python 3k, or maybe later):

    class Foo:
        def self.bar(a,b):
            return a+b
    Foo().bar(1,2) => 3

That way, the declaration would match the invocation (at least
syntactically), and the "magic"-feeling is gone. In the long run, the
"old-style" syntax (i.e. if there's no '.' in the method name) could be
used for static methods.

What do you think?




More information about the Python-list mailing list