tag:blogger.com,1999:blog-4195135246107166251.post7504609865087786721..comments2023-05-08T07:04:09.641-07:00Comments on Neopythonic: Why explicit self has to stayGuido van Rossumhttp://www.blogger.com/profile/12821714508588242516noreply@blogger.comBlogger38125tag:blogger.com,1999:blog-4195135246107166251.post-87904470389077649432015-07-10T04:55:45.808-07:002015-07-10T04:55:45.808-07:00Michael, for a discussion please subscribe and pos...Michael, for a discussion please subscribe and post to python-ideas@python.org.Guido van Rossumhttps://www.blogger.com/profile/12821714508588242516noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-13559193133593130502015-07-09T19:53:33.582-07:002015-07-09T19:53:33.582-07:00I love the cleanliness of Python. No braces, no s...I love the cleanliness of Python. No braces, no semicolons and very few parenthesis. No need to explicitly specify a variable's type or the return value of a function. It is beautiful. Like a calm pond reflecting the sunset. Suddenly, into this pond is tossed a rock called 'self'. The beautiful sunset is spoiled by ripple after ripple, slowly expanding to disrupt every corner of the once tranquil pond.<br /><br />My 11 year old son has been learning Python as his first language. Tonight he asked me, "Daddy, why is all my code 'self', 'self', 'self'"? I decided that was a good question, so I am making a post. Here is a method from one of his classes. Keep in mind that this code is written by an 11 year old.<br /><br />def keep_moving_gravity(self):<br /> if self.key_pressed == 1:<br /> if self.gravity_up == 1:<br /> if self.y > 0:<br /> self.y -= 1<br /> if self.y == 0:<br /> self.key_pressed = 0<br /> else:<br /> if self.y < height:<br /> self.y += 1<br /> if self.y == height:<br /> self.key_pressed = 0<br /><br />Nearly every line contains the word 'self'. Even worse, he keeps muttering curses under his breath as he alternately forgets to use 'self' and then accidentally uses it where he didn't mean to, wasting countless hours. My son is drowning in 'self's. It appears that I will have to go searching for another first language. So sad because Python is so good except for this incredibly painful wart.<br /><br />From my admittedly limited experience with Python, I believe the most pythonic design would have been to make 'self' a keyword that is only required when the scope requires clarification, just like 'global'. At the top of a method, one would simply state 'self x', and for the rest of that method, all references to 'x' refer to a field on the class. No need for an explicit 'self' method parameter and no need to sprinkle 'self.' throughout the code.<br /><br />I am not addressing non-variables such as decorators, because they are not nearly as important as fields. However, I am sure a suitable solution could have been found -- even just requiring the 'self.' prefix on each decorator would be fine because their use is relatively rare.<br /><br />Because I don't like making a criticism without also making a constructive suggestion, here is something that can be done to mitigate the egregious method pollution by "self." without breaking backward compatibility:<br /><br />1. 'self' is not a keyword (no change)<br />2. methods still require an explicit 'self' parameter whose name can be chosen by the programmer (no change)<br />3. the explicit first parameter name chosen by the programmer can be used to scope variables as follows:<br /><br /> class Foo:<br /> def do_something(self, param)<br /> self x # declares that 'x' references are scoped to 'self' (similar to 'global')<br /> x = 5 # same as self.x = 5<br /> def do_something_else(this, param)<br /> this x # declares that 'x' is a member of 'this' (note that any name may be chosen)<br /> x = 5 # same as this.x = 5<br /> def do_yet_another_thing(its, param)<br /> its.x = 5 # still works just like it used to<br /><br />I submit this proposal for consideration in a future version of Python 3.x. It is a small change to the language that provides a huge improvement in readability and is fully backward compatible with existing Python code.Michael Hewitthttps://www.blogger.com/profile/06364720983559894370noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-47750867931856003472013-12-31T15:25:41.562-08:002013-12-31T15:25:41.562-08:00@Michael lovett
I absolutely agree, and I'm n...@Michael lovett<br /><br />I absolutely agree, and I'm not convinced at all that a decorator couldn't infer when self should be passed or not, which is Guido's claim. <br /><br />My vote is for an implicit self parameter, and self being a keyword. Corn8Bithttps://www.blogger.com/profile/00661673153003482883noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-27253116780566121562013-08-12T12:45:01.553-07:002013-08-12T12:45:01.553-07:00I don't like explicit self. I've never see...I don't like explicit self. I've never seen a case where it was to my advantage to specify "self", but on the contrary have had several instances where it was a pain when I forgot it.<br /><br />I also don't buy "explicit is better than implicit". There are plenty of things the language does implicitly that we don't complain about and would be horrified if we had to be explicit. Some cases in point:<br /><br />You can declare a local variable right now without a keyword. You don't have to say <b>local foo = "bar"</b>, the language infers that you want a local variable from scope. <br /><br />Along these same lines, why can't the language infer "self" as well?<br /><br />Anonymoushttps://www.blogger.com/profile/08185431616303637136noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-41437687217713163692013-03-19T20:33:07.116-07:002013-03-19T20:33:07.116-07:00Coming from using Java for the past year or two, I...Coming from using Java for the past year or two, I've found that the only reason I would have needed to use self-references is when I wrote redundant variable names. I've literally never required the use of such a thing in Java because I don't reuse variable names in the same scope.<br /><br />The larger issue I have with people getting snooty about stuff like this though, is that we're talking about an Interpreted language which abstracts most of the low-level stuff away from the programmer anyway. It seems to me that being anal-retentive about a self-reference is a splitting hairs somewhat. Also, Python doesn't support overloading, so let's step back for a moment:<br /><br />A: Python doesn't support overloading, so you can't pull a Java and call self(args) from an overloaded constructor.<br /><br />B: Using self allows you to re-use variable names, which -- and this is my opinion of course -- I think is a bad practice to begin with.<br /><br />I like python, but I personally chalk up explicitly passed "self" references on the "weird idiosyncratic stuff programmers think is a good idea" board where Python is concerned -- right next to the "why the hell did Sun think it was a good idea to make everyone write System.out.print() for every console print statement" entry.Unknownhttps://www.blogger.com/profile/15862504443153420894noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-58666036466767016362012-08-11T01:12:16.795-07:002012-08-11T01:12:16.795-07:00self is useless.
the arguments presented here are...self is useless.<br /><br />the arguments presented here are irrelevant.Cédric Baudryhttps://www.blogger.com/profile/12049086009439355569noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-19549719387663444402012-04-25T20:55:29.990-07:002012-04-25T20:55:29.990-07:00In my opinion, this aspect of python really just e...In my opinion, this aspect of python really just encourages me to write procedural code if/when I use python.(which may or may no be a good thing).<br />Why write <br />class Foo:<br /> def bar(self,arg):<br /> *code*<br /><br />when I can just write<br />def foo_bar(foo,arg):<br /> *code*Richhttps://www.blogger.com/profile/10932702281706641499noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-90378191511086129522012-02-06T02:12:42.987-08:002012-02-06T02:12:42.987-08:00I kinda like the proposal of
def a.b(*args)
auto...I kinda like the proposal of<br /><br />def a.b(*args)<br /><br />automatically turning into<br /><br />def b(a, *args)<br /><br />This would make the dot-syntax more general and I would have a symmetry between calling methods and creating them.<br /><br />Note however, that this would mean, that people would expect things like the following to work, too:<br /><br />def a.b.c() <br /><br />which would be equal to<br /><br />def c(a, b)<br /><br />and this might create some unclear situations, when calling a function from a module:<br /><br />import a.b<br />a.b.c()<br /><br />which would not be equal to <br /><br />from a.b import c<br />c()<br /><br />a.b.c() == c(a,b) != c()<br /><br />(since in the first case, the function would get the package and module as arguments and in the second it would not)<br /><br />And I feel that making this consistent, too, would require quite some changes in Python.<br /><br />So I would prefer to have an improved Error message: <br /><br />If a method I call misses one parameter and the defined method does not have self as first parameter, tell me:<br /><br />“TypeError: b() takes n arguments (n+1 given). Did you add self as first argument?<br /><br />Example:<br /><br />>>> class A:<br />... def b():<br />... pass<br />>>> a = A()<br />>>> a.b()<br />Traceback (most recent call last):<br /> File "<stdin>", line 1, in <module><br />TypeError: b() takes no arguments (1 given). Methods need self as first argument.ArneBabhttps://www.blogger.com/profile/16449390422848764481noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-91516257975299727242010-09-27T14:54:15.426-07:002010-09-27T14:54:15.426-07:00Coming from the PHP "language", we have ...Coming from the PHP "language", we have far worse problems than the explicit self required in Python.<br /><br />Compared to Groovy, and especially Ruby, the so-called elegant language, Python is the absolute natural choice for a sick-of-PHP developer.<br /><br />Nonetheless, this one small issue will drive me nuts, an entire application overflowing with an apparently optional self method param -- uggghhh, if only, please, we the numberless want-to-convert-to-python beings beg of you, get rid of the explicit self method param or I'll...learn Ruby, even @ and @@ is better than explicit self.<br /><br />Seriously, everyone is still using Python 2, just sneak out the explicit self in 3.2 stable and nobody will notice.<br /><br />Alright, alright, money, money talks, how much, in Euros, will it take to get explicit self removed??Unknownhttps://www.blogger.com/profile/10284979265270096076noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-64718976593460409712009-02-06T10:11:00.000-08:002009-02-06T10:11:00.000-08:00Excellent post.Love the BDFL's responsiveness.I th...Excellent post.<BR/><BR/>Love the BDFL's responsiveness.<BR/><BR/>I think the error message as it is...<BR/><BR/>TypeError: m2() takes exactly 3 arguments (2 given)<BR/><BR/>...says what it needs to say: any thinking person would go find out what arguments s/he missed and correct.wickedbeasthttps://www.blogger.com/profile/13914663226374362327noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-41157001613266484502008-12-08T21:40:00.000-08:002008-12-08T21:40:00.000-08:00I'd rather not have 'def self.meth(args)' because ...I'd rather not have 'def self.meth(args)' because it would close off the possibility of generalizing the def statement to allow an arbitrary lvalue in place of the name. This would be useful for things like setting up a dictionary of functions:<BR/><BR/>funcs = {}<BR/><BR/>def funcs['a'](args):<BR/> ...<BR/><BR/>def funcs['b'](args):<BR/> ...Greg Ewinghttps://www.blogger.com/profile/08643537253424047118noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-57905909750339030962008-11-17T08:48:00.000-08:002008-11-17T08:48:00.000-08:00I'd propose splitting the error message into two d...I'd propose splitting the error message into two distinct error messages:<BR/><BR/>TypeError: m2() takes exactly 3 arguments (2 given)<BR/><BR/>Could become:<BR/><BR/>TypeError: unbound method m2() takes exactly 3 arguments (2 given)<BR/><BR/>and <BR/><BR/>TypeError: bound method m2() takes exactly 2 arguments (1 given)<BR/><BR/>(WV: ainsheti: "My proposal isn't great, but it ainsheti either.)Unknownhttps://www.blogger.com/profile/01704275218389858364noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-86753393851020811232008-11-08T21:53:00.000-08:002008-11-08T21:53:00.000-08:00I like the explicit 'self' in the body. Had a litt...I like the explicit 'self' in the body. Had a little concern of it in the argument list.<BR/><BR/>It seems to me Guido may consider the following syntax:<BR/><BR/>def self.foo(arg): ..."<BR/>def cls.foo(arg): ...<BR/><BR/>I want to say that: I LOVE this syntactic sugar and hope it could be considered to be implemented sooner.Unknownhttps://www.blogger.com/profile/15253391504403399271noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-88881549857961224522008-11-04T17:02:00.000-08:002008-11-04T17:02:00.000-08:00I think it makes sense to have an explicit self wh...I think it makes sense to have an explicit self when grafting random methods onto classes since they presumably are not in the lexical scope of the class. When they are contained in a class block I don't see why you would need to define self.<BR/><BR/>It seems like self should always be the bound to the instance of the surrounding class the method is defined in. So a global def has no implicit self but a method does.<BR/><BR/>To keep things from getting lame like javascript closures would include self. The only problem is nested classes? How do you refer to an outer self?<BR/><BR/>def self.meth() seems unnecessary.Anonymoushttps://www.blogger.com/profile/14915510837550321048noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-52806114927441238532008-11-02T11:15:00.000-08:002008-11-02T11:15:00.000-08:00My apologies if this has already been stated, but ...My apologies if this has already been stated, but chromakode on Reddit.com had some very good comments that I think nicely summarize how explicit self helps you out.<BR/><BR/>Here is the entire comment (minus a couple of sentences that pertained to a reply to an earlier comment)<BR/><BR/>"""Python's use of 'self' as an explicit argument is a slight syntactic trick that extremely cleverly glues together the bound/unbound programming experience. Having programmed in many OOP languages where 'this'/'self' are implicit, I have to say that I greatly prefer Python's way of doing it. It answers the following questions very elegantly:<BR/><BR/><B>How did self get defined locally in my method?</B><BR/><BR/>Explicit: you specified it as an argument, ether via instance.meth(args) or class.method(instance, args).<BR/><BR/>Implicit: I put it there for you automatically because you called a class instance method.<BR/><BR/><B>So how do I specify 'self' myself?</B><BR/><BR/>Explicit: you pass it as an argument.<BR/><BR/>Implicit: you use a language construct such as method.apply(instance, args).<BR/><BR/><B>How do I pass around bound methods (or more general: closures) for callbacks?</B><BR/><BR/>Explicit: Evaluating instance.method results in a bound method that calls method(instance, args). Notice how this syntax applies to normal invocations too... ( instance.method(args) <=> (instance.method)(args) ) == method(instance, args)<BR/><BR/>Implicit: You'll typically have to store 'self' on your own in a closure and use the language construct from above to call the method using it."""Anonymoushttps://www.blogger.com/profile/11442714620753651697noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-9114173354217771282008-10-30T10:39:00.000-07:002008-10-30T10:39:00.000-07:00Shouldn't the problem of forgetting typing self be...Shouldn't the problem of forgetting typing self be easily addressed by the IDEs' Intellisense?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-47740161518260264642008-10-28T20:51:00.000-07:002008-10-28T20:51:00.000-07:00I always loved the explicit nature of self yet I r...I always loved the explicit nature of self yet I remember myself beign preferable to a "def self.foo(arg)" syntax in the beginning.<BR/><BR/>I think what disturbs people the most is the optical 'verbosity' it causes. You have your normal arguments in one place, you dont want this special argument together(eg counting your arguements, doest feel nice) <BR/><BR/>But now, I simply add two spaces after my self's comma, just to visibly unbound it a little from the other args, and I think its the best solution overall.<BR/><BR/>Just my 2 cents of a worthless dollar of nowdays to the topic.jimhttps://www.blogger.com/profile/16460570367675013874noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-40663576654186721672008-10-28T06:17:00.000-07:002008-10-28T06:17:00.000-07:00I like explicit self, too. Just thought I'd point ...I like explicit self, too. Just thought I'd point out that Python is flexible enough that if you don't like explicit self, you can just use the Selfless metaclass:<BR/><BR/>http://www.voidspace.org.uk/python/articles/metaclasses.shtmlKevin Dangoorhttps://www.blogger.com/profile/13682774192456462795noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-49156183842625357402008-10-27T09:42:00.000-07:002008-10-27T09:42:00.000-07:00Personally, I like explicit self. The general idea...Personally, I like explicit self. The general idea I get reading comments about it here and elsewhere is that people that are new to python generally don't like it and people that are used to python generally do.<BR/><BR/>That probably means something.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-74949032300622857982008-10-27T05:14:00.000-07:002008-10-27T05:14:00.000-07:00I like the explicit self, and don't think `self' s...I like the explicit self, and don't think `self' should be mandated, e.g. `my' avoids some clutter. BTW, it seems the code that's colourising your Python is treating `self' as a reserved word! Did Bruce write it? :-)Ralph Corderoyhttps://www.blogger.com/profile/13140975971019765573noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-32549352343379419372008-10-27T04:13:00.000-07:002008-10-27T04:13:00.000-07:00After having programmed in Python for quite a whil...After having programmed in Python for quite a while, I actually <B>miss</B> the explicitness of self in other languages.<BR/><BR/>It really has strong "say what you mean" semantics -- taking a superficial glance at the code, you can at once see whether instance or non-instance variables are accessed. Compare this with using markup (prefixing _ to member vars) to relay the meaning -- you have to know the conventions to grasp the difference, i.e. there is an extra level in comprehension (aha, _foo denotes this.foo). It's not uncommon to see people use explicit <I>this</I> in Java and C++ as well for that particular reason.<BR/><BR/>I'd say most people who grok Python at the "idiomatic" level instead of using it as an easier way to write Java would be seriously upset if <I>self</I> is abandoned.<BR/><BR/>Hopefully nothing like that will ever happen.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-30329648798256445432008-10-27T03:01:00.000-07:002008-10-27T03:01:00.000-07:00Method poking is a non-issue. For functions define...Method poking is a non-issue. For functions defined outside class require explit self.<BR/><BR/>I'd realy like explicit self in class definitions to be removed. It makes OO in Python look like an ugly after-thought/hack.senior noisehttps://www.blogger.com/profile/00741594083419251417noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-19122123415001999512008-10-27T02:32:00.000-07:002008-10-27T02:32:00.000-07:00The explicit self is wonderful. Instead if wonderi...The explicit self is wonderful. Instead if wondering why you have to type this in Python, I've always wondered why you don't have to in other languages. It takes away the implicit "this" magic. Self makes perfect sense.M-MZhttps://www.blogger.com/profile/00848009815968294197noreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-56249337525956642782008-10-27T02:26:00.000-07:002008-10-27T02:26:00.000-07:00I like self, the 'wrong number of arguments' error...I like self, the 'wrong number of arguments' error message to me now parses as 'you missed self again'<BR/><BR/>I like that it forces you to see objects and methods as non magical beasts.<BR/><BR/>Would a good editor that auto completes methods solve this problem? Hack IDLE to do this? :-)<BR/><BR/>BTW why is it 'self' and not 'this'?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4195135246107166251.post-60029555093728902572008-10-27T01:54:00.000-07:002008-10-27T01:54:00.000-07:00I really like the explicit "self"... to the point ...I really like the explicit "self"... to the point that I hate the implicit "super".Unknownhttps://www.blogger.com/profile/11957653131806024920noreply@blogger.com