3 ms·
A language that forces me to say "self" every other word doesn't strike me as particularly clever. Especially when that keyword is not necessary and when it br
by billmcneale 16y ago
A language that forces me to say "self" every other word doesn't strike me as particularly clever.
Especially when that keyword is not necessary and when it breaks standard programmer expectations (if I declare a method with 3 parameters, I should call it with 3, not 2).
- code_duck 16y agoI like 'self'. I don't mind it at all. The error messages related to `self` in method definitions caused me some confusion when starting out with python. For instance if you define a method with no arguments, calling it results in "TypeError: your_method() takes no arguments (1 given)".
- xiongchiamiov 16y agoAh, but you are calling it with 3 parameters: foo.bar(baz, sputz) ^1 ^2 ^3 When you write a class method, it has more information available to it than a similar subroutine; thus, another parameter. GvR brings up the deeper reasons for explicit self at http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html http://neopythonic.blogspot.com/2008/10/why-explicit-self-ha... .
- billmcneale 16y agoYes, this is exactly what I mean by "breaking programmer's expectations". Except maybe Modula, no language works like that at all. The parameters are in the parentheses, period. If you need to pass this, you do so in the declaration and in the invocation. If this is passed implicitly, you don't declare it in the parameters and you don't pass it at the call site. Python is doing this totally weird stuff that sits in the middle and that makes no logical sense at all. The fact that Python forces you to declare the "self" parameter is simply due to the fact that it's old, old, old. Nothing wrong with that, but post rationalizing it by saying it's okay to declare a method with 3 parameters but calling with just 2 is just silly.
- xiongchiamiov 16y agoSimply because it does not fit your expectations does not mean that it makes "no logical sense at all". As faulty beings, we often have expectations that really are quite far from logical.
- fleitz 16y agoThis is why I love F#. It lets you define the name of the this parameter in exactly the same way you pass it. type Bar(p) = member x.Foo(bar) = printf "%s %d" bar p let x = new Bar(10) x.Foo("Foo bar") Output: Foo bar 10 And yes, the variables bar and p are type inferred from the %s and %d respectively
- code_duck 16y agoYou can rename self to whatever you want in Python by changing it in the function definition. class Flip(object): foo=4 def flop(x,num): x.bar=x.foo+num
- xiongchiamiov 16y agoRight, but his point was that the self variable is declared in front of the method name, similarly to how it looks when you call said method.