4 ms·
> Yeah, like dealing with "this" keyword or type coercion in JS is not the ugliest thing ever in the history of Programming languages Try python. Python's hand
by mschuetz 10y ago
> Yeah, like dealing with "this" keyword or type coercion in JS is not the ugliest thing ever in the history of Programming languages
Try python. Python's handling of this/self makes javasacript look like the most amazing thing ever.
- krzyk 10y agoWhat do you mean? Could you give an example? I always regarded python self to be one of the cleanest ways to show that we have another parameter in a method.
- _ZeD_ 10y agoare you joking, right? in javascript you cannot ever KNOW what this refers to (is the instance of a new Thing()? is null? is "window"? is something set with .bind()? is just "the left thing before the dot before the function"? is something else?
- Sohcahtoa82 10y agoDoes "this" work differently in JS as it does from Python or even C++? Is it not always the object that owns the function currently being executed? If the "this" is implicit, then how can you possibly know where the variable comes from?
- tastyham 10y agoEh. Python and JavaScript are very similar at the language level. They each have warts and are more expressive in certain areas, but at a high level they have the same semantics and the exact same advantages and disadvantages.
- scriptkiddy 10y agoCould you please explain? It's always very clear in Python what `self` refers to. Self is always an instance of the parent object of a method. Some classes can also have static class methods that can be called from the class itself instead of one of it's instances. Maybe that is where your confusion is arising?
- StavrosK 10y agoClass methods don't accept a "self" argument, though. In those cases, you call it cls.
- scriptkiddy 10y agoI'm aware. The `self` vs `cls` thing is convention. You can call either argument whatever you want.
- mschuetz 10y agoIt's not that it's confusing, rather it's unnecesarrely cumbersome because it's explicit and because you have to put it as an argument. Looking at the "most used words" [1] list of python, self is the #1 used word by far, occuring three times as often as the second most common word, "if". At this point, it's just a filler that bloats code and makes it less readable in many cases # so much text for such a little task def length(self): return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z) # better, but not quite there yet def length(self): return math.sqrt(self.x**2 + self.y**2 + self.z**2) # this would be perfect, but it's not allowed by python def length(): return math.sqrt(x*x + y*y + z*z) To be fair, javascript also forces you to explicitly specify "this" so there is no difference in that regard. What I find worse than that is that you have to specify self in the parameter list. Calling a function with 2 parameters but having to declare 3 doesn't make sense. The usuall reasoning is that "explit is always better than implicit" but I do not agree. We could take this philosophy all the way to explicitly defining all the global variables that may be used by the function in the parameter list, maybe even all the packages that this function is going to use. We don't do that for a good reason. [1] https://anvaka.github.io/common-words/#?lang=py https://anvaka.github.io/common-words/#?lang=py
- scriptkiddy 10y ago> It's not that it's confusing, rather it's unnecesarrely cumbersome because it's explicit and because you have to put it as an argument. I don't feel the same way. To each their own I guess. Regarding your example, I don't see anything wrong with the second version. Anyone who reads the code will easily be able to tell what it does and where the variables come from. In your third example, how is anyone supposed to know whether or not `x`, `y`, and `z` are class variables or global variables? Kind of defeats the purpose of namespacing, I think. > What I find worse than that is that you have to specify self in the parameter list. This is really one of Python's implementation details. Methods can be bound or unbound. Without going into to much detail, just know that a bound method can be used as a regular function. Here is some code to illustrate what I mean: >>> class A(): ... def a(self): ... print('hello') ... >>> A <class '__main__.A'> >>> A.a <function A.a at 0x7fc4009381e0> >>> A.a() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: a() missing 1 required positional argument: 'self' >>> A().a() hello >>> b = A() >>> A.a(b) hello >>> class B(): ... pass ... >>> b = B() >>> A.a(b) hello >>> class B(): ... def a(self): ... print('goodbye') ... >>> b = B() >>> A.a(b) hello >>> c = A() >>> B.a(c) goodbye Self does not have to be the same class it's called on. It can be any class. However, if you try to access a class attribute that doesn't exist, you get an attribute error. >>> class C(): ... x = 10 ... def a(self): ... print(self.x) ... >>> C.a() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: a() missing 1 required positional argument: 'self' >>> C().a() 10 >>> a = A() >>> a.a() hello >>> C.a(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 4, in a AttributeError: 'A' object has no attribute 'x' Since bound methods need to behave like regular functions, it is improper to not pass the object instance to the function.