4 ms·
I find the self in the method call parameters slightly awkward, but other than that I like writing a lot of 'self'. One of the biggest reasons I prefer Python t
by ditonal 12y ago
I find the self in the method call parameters slightly awkward, but other than that I like writing a lot of 'self'. One of the biggest reasons I prefer Python to Ruby or Perl is that I really agree "Explicit is better than implicit.", and adding the self before your instance variables and method calls explicitly labels their namespace/scope, and I don't find it too distracting. I sometimes do it in Java too, prefixing instance variables and method calls with 'this'.
I do agree that OOP approaches can be a bit overused (especially if someone's been coding too much Java), and I've always appreciate that Python doesn't shove it down your throat. I like that you can lean towards more functional code like this example, or towards simple imperative code. But when I do want some classes, I find the Python mechanism mostly ok.
- csirac2 12y agoFor what it's worth, the perl object system was actually borrowed from Python, so perl code is full of $self (or $this) too.
- AjithAntony 12y agoNeat, I expected it was the other way around: " I don't really know much about Python. I only stole its object system for Perl 5. I have since repented." Larry Wall http://www.perl.com/pub/2007/12/06/soto-11.html http://www.perl.com/pub/2007/12/06/soto-11.html
- panzi 12y agoI like the self parameter, because it makes the language more powerful. You can now simply assign a function to a class and make it a member method if it accepts an appropriate first parameter. The opposite is equally true. You can do something like `map(Class.method, sequence)`.
- agumonkey 12y agoIn javascript you can rebind functions to any context without explicit self in signature. I admit this (PNI) was deeply confusing at first but I tend to prefer this way now. It feels a simpler and more general abstraction.
- panzi 12y agoBut then getting a reference to a bound method is simpler in Python: bound_method = object.method In JavaScript one would have to write: var bound_method = object.method.bind(object);
- panzi 12y agoAlso for unbound methods to make them usable as functions. Python: func = Foo.method JavaScript: var func = Function.prototype.call.bind(Foo.prototype.method);
- 3pt14159 12y agoIn Ruby we use an @ / self for both instance variables (mandatory, use self to call the getter / setter and @ to interact with the variable directly) and methods (self optional, generally used to reduce confusion if there might be multiple things with the same name).
- NateDad 12y agoI like it in Go, too. Go requires it, the way python does, but the convention is to use a real variable name rather than self, since there's no real difference between that the function receiver variable and the test of the function arguments. I also like the explicit name spacing.
- themoonbus 12y agoI agree. I even always have a "var self = this" in my JavaScript class methods so I don't ever have to worry about scope of "this".