4 ms·
> ...the “implied self parameter” on python methods turned me off. If I had a nickel for every time I got the "TypeError: random_function() takes 1 positional
by UncleEntity 8y ago
> ...the “implied self parameter” on python methods turned me off.
If I had a nickel for every time I got the "TypeError: random_function() takes 1 positional argument but 2 were given" because I forget the "self" parameter I could buy myself a coffee. Probably a fancy one from the Starbucks even!
- fiddlerwoaroof 8y agoI always thought this made sense: a method, considered as a property of a class isn’t yet bound to any particular instance while, considered as a property of an instance has an implicit parameter of the instance you’re currently operating on: languages like Java hide this “this” parameter from you by passing it automatically while python makes it explicit so you can form a better mental model of what’s going on
- zwp 8y agoThe problem is that Python is the exception here. Those of us that learnt other languages first already have a mental model and find this explicit self parameter weird. And it's more typing. Yegge thinks that "self" is a wart and I agree. The article is a fun read but it's showing its age. I suspect Python might just have beaten Perl by now. https://sites.google.com/site/steveyegge2/tour-de-babel#TOC-Python https://sites.google.com/site/steveyegge2/tour-de-babel#TOC-... I suppose Python's way there's one less reserved word. Are there any other advantages?
- fiddlerwoaroof 8y agoI also like how you can call the method on the class and pass self in explicitly. e.g.: class Foo: b = 4 def foo(self, a): return a + self.b inst = Foo() inst.foo(3) Foo.foo(inst, 3) I haven't written python in a while, but I really like that python makes this possible and obvious and I've found this behavior pretty handy when working in a more functional style.
- fiddlerwoaroof 8y agoAlso, I find writing languages with an implicit self like Java, Kotlin and Scala really irritating because there's nothing to distinguish access to a locally-scoped variable or function from access to a class member. I've always appreciated how, in Python, `x` always refers to something bound according to Python's pseudo-lexical scoping rules while class members always have a `self.` to make them stand out. These days, however, I mostly write Common Lisp and Clojure which, each in their own way, have explicit selfs: Common Lisp because it uses multiple dispatch and, consequently, there isn't one "self" and Clojure's protocols are designed to dispatch on the first argument, which is always passed explicitly.
- user234683 8y agoI think the way Lua handles it is much better. If you have object foo of class Foo with method bar, accessing it like foo.bar does no magic on it. You would have to call it as foo.bar(foo, ...) if you wanted to access the method with a dot. Only foo:bar will do the magic on it. If you call it as foo:bar(...) then the object will be passed to the first parameter. This follows "explicit is better than implicit" much better than the implicit self parameter in my opinion.
- fiddlerwoaroof 8y agoI'm no Lua expert, but isn't this because Lua's object model is more prototypical inheritance than a standard class-based model like python has?
- njharman 8y agoIt's not implied, it's explicit in Python. you have to put it right there in the method signature! And it's obvious when python passes it in cause you used dot syntax! Other languages have implied "this" or "self". class LiterallyThere: def method(self, foo): "Literally explicit, self" Dot operator means apply this method to thing left of dot by executing method with thing on left as first arg. LiterallyThere.method('foo') passes the class object LitterallyThere as the first arg lt = LiterallyThere() lt.method('foo') passes the instance of the class object represented by lt as the first argument. method(something, 'foo') If you were able to reference method without a dot, you would have to supply two arguments cause method takes two arguments. You literally, explicitly have to tell the interpreter what you are passing as first parameter and you method signature has to literally, explicitly accept that parameter.
- Rotareti 8y agoThere are linters for these kinds of things that work with most editors. E.g. VSCode with pylint marks the method immediately, if you forget `self` or `cls`.