Proposal for removing self

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brent W. Hughes

    Proposal for removing self

    When doing object-oriented stuff, it bothers me to have to type "self" so
    many times. I propose that Python allow the programmer to optionally type
    ".variable" instead of "self.varia ble" to mean the same thing. Of course,
    the interpreter would have to be more careful about detecting floats that
    begin with just a period as in ".5". What are your thoughts?


  • Roger Binns

    #2
    Re: Proposal for removing self

    Brent W. Hughes wrote:[color=blue]
    > When doing object-oriented stuff, it bothers me to have to type
    > "self" so many times. I propose that Python allow the programmer to
    > optionally type ".variable" instead of "self.varia ble" to mean the
    > same thing. Of course, the interpreter would have to be more careful
    > about detecting floats that begin with just a period as in ".5".
    > What are your thoughts?[/color]

    When I started doing Python, it annoyed me. Now many years later
    I definitely wouldn't change it. Guido has very good taste!

    Roger


    Comment

    • Raymond Hettinger

      #3
      Re: Proposal for removing self

      "Brent W. Hughes" <brent.hughes@c omcast.net> wrote in message news:<T_7Xc.613 60$Fg5.2281@att bi_s53>...[color=blue]
      > When doing object-oriented stuff, it bothers me to have to type "self" so
      > many times. I propose that Python allow the programmer to optionally type
      > ".variable" instead of "self.varia ble" to mean the same thing. Of course,
      > the interpreter would have to be more careful about detecting floats that
      > begin with just a period as in ".5". What are your thoughts?[/color]

      There's a FAQ on the subject:




      Raymond Hettinger

      Comment

      • Raymond Hettinger

        #4
        Re: Proposal for removing self

        "Brent W. Hughes" <brent.hughes@c omcast.net> wrote in message news:<T_7Xc.613 60$Fg5.2281@att bi_s53>...[color=blue]
        > When doing object-oriented stuff, it bothers me to have to type "self" so
        > many times. I propose that Python allow the programmer to optionally type
        > ".variable" instead of "self.varia ble" to mean the same thing. Of course,
        > the interpreter would have to be more careful about detecting floats that
        > begin with just a period as in ".5". What are your thoughts?[/color]

        There's a FAQ on the subject:




        Raymond Hettinger

        Comment

        • Ravi Teja Bhupatiraju

          #5
          Re: Proposal for removing self

          "Brent W. Hughes" <brent.hughes@c omcast.net> wrote in message news:<T_7Xc.613 60$Fg5.2281@att bi_s53>...[color=blue]
          > When doing object-oriented stuff, it bothers me to have to type "self" so
          > many times. I propose that Python allow the programmer to optionally type
          > ".variable" instead of "self.varia ble" to mean the same thing. Of course,
          > the interpreter would have to be more careful about detecting floats that
          > begin with just a period as in ".5". What are your thoughts?[/color]

          I know that this has been proposed before and am familiar with the
          alternatives commonly suggested. I understand that most Python
          programmers are happy with things the way they are in this respect.
          But after 3 years of Python, somehow, I still keep forgetting to type
          the darn thing. Of course, I always catch the omission quickly but
          that makes me wish there was a better alternative. Python has
          generally been about less typing and self seems somewhat contrary to
          that.

          This following is not a carefully thought one but I would be
          interested to know what your opinions are. The following is inspired
          by win32all.

          <from Python COM docs>
          class HelloWorld:
          _public_methods _ = ['Hello']
          _public_attrs_ = ['softspace', 'noCalls']
          _readonly_attrs _ = ['noCalls']

          def __init__(self):
          pass
          </from Python COM docs>

          How about having an optional attribute for a class that negates the
          requirement for self?

          One advantage with this is I can see all the member variables in one
          place. Great while reading code.

          <sample>
          class FooBar:
          __public__ = [Name, Age]
          def setName(name):
          Name = name
          </sample>

          While I am complaining, a few other peeves ...

          Another thing is I dislike is having to use __ for *every* private
          variable. For *me* __private__ at the top of the class definition code
          would be more elegant. __readonly__ would be another nicety.

          Introducing the above attributes into the language would not likely
          break any existing Python code.

          Just my 2c.

          Comment

          • Pierre-Frédéric Caillaud

            #6
            Re: Proposal for removing self


            There was the "with" keyword :

            with self:
            .member = something
            .member2 = .member3 + .member4

            etc... it is a bit more implicit because of the "with"... anyway.

            Comment

            • Max M

              #7
              Re: Proposal for removing self

              Brent W. Hughes wrote:[color=blue]
              > When doing object-oriented stuff, it bothers me to have to type "self" so
              > many times. I propose that Python allow the programmer to optionally type
              > ".variable" instead of "self.varia ble" to mean the same thing. Of course,
              > the interpreter would have to be more careful about detecting floats that
              > begin with just a period as in ".5". What are your thoughts?[/color]


              You don't have to use self. You could simply use underscore '_' instead.

              class Person:

              def setName(_, name):
              _.name = name

              def getName(_):
              return _.name


              But other Python programmers will hate you for it ;-)


              If it is because you are using an attribute too many times inside a
              method, a better solution is to map it to a local variable. It also has
              the advantage of being faster.


              class SoftwareProject Estimator:

              pi = 3.14

              def guestimatePrice (self, hours, hour_price, developer_group _size):
              pi = self.pi
              guess = hours * pi
              guess *= pi * developer_group _size
              return guess * hour_price


              regards Max M

              Comment

              • Alex Martelli

                #8
                Re: Proposal for removing self

                Andrea Griffini <agriff@tin.i t> wrote:
                [color=blue]
                > On 25 Aug 2004 22:23:46 -0700, rattan@cps.cmic h.edu (Ishwar Rattan)
                > wrote:
                >[color=green]
                > >I concur for keeping self intact, it does provide clarity in code writing
                > >and reading.[/color]
                >
                > I remember Alex Martelli advocating in our italian newsgroup on
                > C++ that C++ programmers should use "this->..." everywhere :-)[/color]

                I did that, and I did that before becoming mad with love for Python.

                At the time my main job was helping hundreds of other programmers debug
                their hairiest bugs (also helping them architect and design and test
                things correctly, but debugging took far more time -- there's never time
                to do it right so there's always time to do it over) and the lack of
                this-> was a major cause of problems. confusions and bugs.
                [color=blue]
                > Seemed crazy to me at that time, but now after giving a try
                > to python I've to say that self-consciousness isn't bad.[/color]

                If you write and debug truly complicated C++ templates you'll see why I
                wanted explicit this-> prefixing most ardently...;-).


                Alex

                Comment

                • Alex Martelli

                  #9
                  Re: Proposal for removing self

                  Ravi Teja Bhupatiraju <webraviteja@ya hoo.com> wrote:
                  ...[color=blue]
                  > <sample>
                  > class FooBar:
                  > __public__ = [Name, Age]
                  > def setName(name):
                  > Name = name
                  > </sample>[/color]

                  Go ahead: learn about metaclasses and bytecode hacking and write your
                  custom metaclass to perform this. Except for the little detail that
                  you'll never get away with not quoting 'Name' and 'Age' in the
                  __public__ assignment, the rest is reasonably easy to perform. Publish
                  your metaclass and start campaigning for it.

                  As long as you're just yakking about it ain't gonna happen -- become a
                  metaclass and bytecode expert and you can implement this in Python, or
                  convince somebody who is such an expert, for which the implementation
                  should be the job of a couple days tops.

                  [color=blue]
                  > While I am complaining, a few other peeves ...
                  >
                  > Another thing is I dislike is having to use __ for *every* private
                  > variable. For *me* __private__ at the top of the class definition code
                  > would be more elegant. __readonly__ would be another nicety.[/color]

                  That's an even easier job for a custom metaclass with very modest amount
                  of byecode hacking. I suggest you start with this one, really truly
                  VERY easy. In fact you could probably do it without any actual hacking
                  on the bytecode itself, just rewriting a few tables of strings in the
                  code objects of the functions that are the methods of the class.

                  One impossible task (THAT would require hacking the Python parser and
                  changing things very deeply) as I already mentioned would be the form
                  you want for the assignment to __public__ (or similarly to
                  __private__etc) . Those names are undefined (or worse defined globally)
                  and to teach Python (and human readers!) that for that one speclal case
                  of assignment to __public__ (or __private__), THAT one case only,
                  COMPLETELY different rules apply than ANYWHERE else in Python, well,
                  it's just as difficult as it is absurd.

                  I suggest you tackle the 'look ma no quotes!' task last, after
                  everything else is working, because that one detail is going to take far
                  longer than everything else put together, even if you're learning about
                  metaclasses and bytecodes from scratch.

                  [color=blue]
                  > Introducing the above attributes into the language would not likely
                  > break any existing Python code.[/color]

                  If this peculiar behavior is limited to classes whose metaclass is your
                  custom RaviSpecial, "not likely" becomes "absolutely impossible", a
                  great enhancement. One more reason to implement this in a metaclass.
                  [color=blue]
                  > Just my 2c.[/color]

                  Well make it three cents and do an implementation, if you really care.
                  Otherwise, it's just words...


                  Alex

                  Comment

                  • Jacek Generowicz

                    #10
                    Re: Proposal for removing self

                    "Brent W. Hughes" <brent.hughes@c omcast.net> writes:
                    [color=blue]
                    > When doing object-oriented stuff, it bothers me to have to type "self" so
                    > many times.[/color]

                    When doing object-oriented stuff, it makes me extactic to write
                    "self." in front of instance attributes. The knowledge that other
                    programmers are always doing the same, makes me even more happy.

                    The fact that there are plenty of C++ and Java coding conventions
                    which require that all class members' names start with "m_", or that
                    all instance attributes be accessed, in methods, via "this->" or
                    "this." (depending) on the language in question, seems to support
                    Python's choice in this matter.
                    [color=blue]
                    > I propose that Python allow the programmer to optionally type
                    > ".variable" instead of "self.varia ble" to mean the same thing. Of course,
                    > the interpreter would have to be more careful about detecting floats that
                    > begin with just a period as in ".5". What are your thoughts?[/color]

                    I think you should get into the habit of reading FAQs, Archives,
                    Googling (you know, the usual stuff), before making such suggestions
                    publically.

                    Comment

                    • Sion Arrowsmith

                      #11
                      Re: Proposal for removing self

                      Max M <maxm@mxm.dk> wrote:[color=blue]
                      >You don't have to use self. You could simply use underscore '_' instead.[/color]

                      When I started using Python for developing my own bits and pieces I
                      got into the habit of using 'I' instead of 'self'. Means pretty much
                      the same thing, stands out better in code, takes up less space on a
                      line, and is two keystrokes shorter. ...
                      [color=blue]
                      >But other Python programmers will hate you for it ;-)[/color]

                      .... And then stopped when I got the job of maintaining other people's
                      Python code and writing stuff that other people would have to use, if
                      not actively maintain.

                      Going back to look at C++ code (whoever wrote it) makes me realise
                      how helpful it is to have an explicit marker as to what's belongs
                      to a class and what doesn't. And before anyone mentions Hungarian
                      notation, (a) is 'self.' *really* that much more of an imposition
                      over 'm_'? and (b) it's not useful for distinguishing methods from
                      functions.

                      --
                      \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
                      ___ | "Frankly I have no feelings towards penguins one way or the other"
                      \X/ | -- Arthur C. Clarke
                      her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

                      Comment

                      • Ravi Teja Bhupatiraju

                        #12
                        Re: Proposal for removing self

                        aleaxit@yahoo.c om (Alex Martelli) wrote in message news:<1gj4sup.h rowbz1bzo06mN%a leaxit@yahoo.co m>...[color=blue]
                        > Ravi Teja Bhupatiraju <webraviteja@ya hoo.com> wrote:
                        > ...[color=green]
                        > > <sample>
                        > > class FooBar:
                        > > __public__ = [Name, Age]
                        > > def setName(name):
                        > > Name = name
                        > > </sample>[/color]
                        >
                        > Go ahead: learn about metaclasses and bytecode hacking and write your
                        > custom metaclass to perform this. Except for the little detail that
                        > you'll never get away with not quoting 'Name' and 'Age' in the
                        > __public__ assignment, the rest is reasonably easy to perform. Publish
                        > your metaclass and start campaigning for it.
                        >
                        > As long as you're just yakking about it ain't gonna happen -- become a
                        > metaclass and bytecode expert and you can implement this in Python, or
                        > convince somebody who is such an expert, for which the implementation
                        > should be the job of a couple days tops.
                        >
                        >[color=green]
                        > > While I am complaining, a few other peeves ...
                        > >
                        > > Another thing is I dislike is having to use __ for *every* private
                        > > variable. For *me* __private__ at the top of the class definition code
                        > > would be more elegant. __readonly__ would be another nicety.[/color]
                        >
                        > That's an even easier job for a custom metaclass with very modest amount
                        > of byecode hacking. I suggest you start with this one, really truly
                        > VERY easy. In fact you could probably do it without any actual hacking
                        > on the bytecode itself, just rewriting a few tables of strings in the
                        > code objects of the functions that are the methods of the class.
                        >
                        > One impossible task (THAT would require hacking the Python parser and
                        > changing things very deeply) as I already mentioned would be the form
                        > you want for the assignment to __public__ (or similarly to
                        > __private__etc) . Those names are undefined (or worse defined globally)
                        > and to teach Python (and human readers!) that for that one speclal case
                        > of assignment to __public__ (or __private__), THAT one case only,
                        > COMPLETELY different rules apply than ANYWHERE else in Python, well,
                        > it's just as difficult as it is absurd.
                        >
                        > I suggest you tackle the 'look ma no quotes!' task last, after
                        > everything else is working, because that one detail is going to take far
                        > longer than everything else put together, even if you're learning about
                        > metaclasses and bytecodes from scratch.
                        >
                        >[color=green]
                        > > Introducing the above attributes into the language would not likely
                        > > break any existing Python code.[/color]
                        >
                        > If this peculiar behavior is limited to classes whose metaclass is your
                        > custom RaviSpecial, "not likely" becomes "absolutely impossible", a
                        > great enhancement. One more reason to implement this in a metaclass.
                        >[color=green]
                        > > Just my 2c.[/color]
                        >
                        > Well make it three cents and do an implementation, if you really care.
                        > Otherwise, it's just words...
                        >
                        >
                        > Alex[/color]

                        Aren't you being a bit touchy Alex? Now I am glad that I did not
                        mention my wish list on properties and DBC :-). However, your post has
                        been quite informative. So I can live with that.

                        I wish I could rise up to your challenge and do all that stuff.
                        Regretfully, my Python skills don't extend to metaclasses and
                        especially "byte code hacking". Maybe you will explain those in your
                        next edition of the cookbook :-).

                        That said, I only could find one set of docs that could be called a
                        <tutorial> on metaclasses. Those were by David Mertz and Michele
                        Simionato. I could not get through them. I took comfort in the
                        statement "Metaclasse s are deeper magic than 99% of users should ever
                        worry about" from the document. I am still looking for something I can
                        understand.

                        Comment

                        • Sean Ross

                          #13
                          Re: Proposal for removing self


                          "Ravi Teja Bhupatiraju" <webraviteja@ya hoo.com> wrote in message[color=blue]
                          > __readonly__ would be another nicety.[/color]

                          Well, you could use this for __readonly__ (though I named it 'readable()'):


                          or if you want to use meta-classes (and this recipe does use __readonly__):


                          There are many similar implementations floating around.

                          As for removing self:


                          Enjoy,
                          Sean


                          Comment

                          • Alex Martelli

                            #14
                            Re: Proposal for removing self

                            Ravi Teja Bhupatiraju <webraviteja@ya hoo.com> wrote:
                            ...[color=blue]
                            > Aren't you being a bit touchy Alex? Now I am glad that I did not
                            > mention my wish list on properties and DBC :-). However, your post has
                            > been quite informative. So I can live with that.[/color]

                            Exactly: far from being "touchy", I have been constructive. Coming back
                            to c.l.py after months of absence, and finding that, as usual, instead
                            of trying to use Python to best effect, people with mediocre Python
                            mastery keep trying to change Python (generally trying to break some of
                            the best aspects of it, because they don't understand enough Python to
                            see why they're good) was of course depressing -- I'm proud of myself
                            for reacting constructively rather than lashing out or going away again
                            at once.

                            [color=blue]
                            > I wish I could rise up to your challenge and do all that stuff.
                            > Regretfully, my Python skills don't extend to metaclasses and
                            > especially "byte code hacking". Maybe you will explain those in your
                            > next edition of the cookbook :-).[/color]

                            Bytecodehacks are implementation-specific, and specific to one version
                            of Python (they wouldn't apply to Jython, IronPython, ...), so I'm not
                            going to write about them -- and you don't need to master them to try
                            out one of the changes you want, the __private__ idea. Metaclasses are
                            quite another issue: they are, even conceptually, absolutely fundamental
                            to understanding Python's object model, and if you do not understand the
                            object model, your knowledge of Python is shallow -- FAR too shallow for
                            you to _sensibly_ propose changes to Python, in my opinion.

                            So I did cover metaclasses in "Python in a Nutshell" as well as in
                            dedicated presentations (you can find the presentations' PDFs on the
                            usual site, www.strakt.com) and I will probably select a few metaclass
                            recipes for the second edition of the Cookbook.

                            [color=blue]
                            > That said, I only could find one set of docs that could be called a
                            > <tutorial> on metaclasses. Those were by David Mertz and Michele
                            > Simionato. I could not get through them. I took comfort in the
                            > statement "Metaclasse s are deeper magic than 99% of users should ever
                            > worry about" from the document. I am still looking for something I can
                            > understand.[/color]

                            At least 99% of users should not propose changes to Python. Many of
                            them would be well served by undersanding metaclasses, even if they
                            never need to write custom metaclasses -- they shouldn't _worry_ about
                            them, since there's nothing to worry about, but understanding and
                            worrying are different things. Which is why I cover metaclasses in
                            books, articles, posts (use google groups to look for my post that
                            mention metaclass, there were quite a few), and presentations.


                            Alex

                            Comment

                            • Ravi Teja Bhupatiraju

                              #15
                              Re: Proposal for removing self

                              "Sean Ross" <sross@connectm ail.carleton.ca > wrote in message news:<gStXc.290 13$DG.1381823@n ews20.bellgloba l.com>...[color=blue]
                              > "Ravi Teja Bhupatiraju" <webraviteja@ya hoo.com> wrote in message[color=green]
                              > > __readonly__ would be another nicety.[/color]
                              >
                              > Well, you could use this for __readonly__ (though I named it 'readable()'):
                              > http://aspn.activestate.com/ASPN/Coo.../Recipe/157768
                              >
                              > or if you want to use meta-classes (and this recipe does use __readonly__):
                              > http://aspn.activestate.com/ASPN/Coo.../Recipe/197965
                              >
                              > There are many similar implementations floating around.
                              >
                              > As for removing self:
                              > http://starship.python.net/crew/mwh/hacks/selfless.py
                              >
                              > Enjoy,
                              > Sean[/color]

                              Thanks Sean. This is interesting code. I should really learn about metaclasses.

                              Comment

                              Working...