Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Guido van Rossum responds to Bruce Eckel: Why explicit self has to stay (neopythonic.blogspot.com)
34 points by soundsop on Oct 26, 2008 | hide | past | favorite | 7 comments


I see where Bruce is coming from.

But I quite like self: it's an explicit reminder every time you write OOP in Python about the idea of classes & instances.

Incidentally I'm one of those annoying people that often will use this instead of self (because I find it more readable.. at work I am famed for my efforts of trying to write code in "english" :D).

Self is also useful when your passing the class around. It's easy to see where it is heading all the time.

It will be nice to see the "you forgot self, you fool" error made explicit. When you switch languages a couple of times a day it can be easy to forget :) (PHP to Python switch is the hardest!)


`my' is also an alternative, can make the code look less cluttered.


The

  def self.foo(arg): ...
variant GvR mentions reminds me of one of the many ways you can implement OO-style in Lua:

  function object.method(arg) ... end


I think you mean function object:method(arg) ... end, if you're assuming that object will be passed (as "self") implicitly. The colon is the difference from normal map indexing.

I much prefer Lua's solution for this over Python's (and I'm also coming to strongly prefer prototype-style OO, in general).


Indeed, you are correct.

  function obj:method(arg) .. end
is the same as

  function obj.method(obj,arg) .. end
I think the reason I mentioned the latter is I generally use Lua closures to code the way Eckel preferrs.

"Objects are a poor man's closures" -- Norman Adams


F# uses it too for adding OO members to types:

    type Foo =
        ...
        member self.Bar(arg) =
'self' can be named as you like.


There are two reasons why i don't mind 'self'. The first one is that python was my first language, so i didn't know anything else. I didn't find it strange, as a person with a different background might. The second reason is that i don't do that much OOP any way, so i don't get frustrated with having to type self all the time. But when i do actually write OO code, i can see how self might annoy someone writing classes and methods all day.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: