Why the 'self' argument?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grzegorz Staniak

    Why the 'self' argument?

    Hello,

    I'm a newbie Python user, a systems administrator - I've been trying
    to switch from Perl to Python for administrative tasks - and one thing
    I cannot understand so far is why I need the special 'self' (or anything
    esle) argument in class method definitions. I might have missed an
    explanation in the docs, a quick Google search did not help. Is there
    somewhere on the web you could direct me to?

    Thanks,

    --
    Grzegorz Staniak <gstaniak@zagie l.com.pl>
  • Grant Edwards

    #2
    Re: Why the 'self' argument?

    In article <vlh7otscuo8gb1 @news.supernews .com>, John Roth wrote:
    [color=blue]
    > Technically, it would be possible to make "self" a reserved
    > word, and not have to put it in the method declaration.
    > However, there are a lot of people who use something other than
    > the word "self," so that would break existing code.[/color]

    It would also make the language more complex and irregular.

    --
    Grant Edwards grante Yow! Could I have a drug
    at overdose?
    visi.com

    Comment

    • Gerhard Häring

      #3
      Re: Why the 'self' argument?

      Grzegorz Staniak wrote:[color=blue]
      > Hello,
      >
      > I'm a newbie Python user, a systems administrator - I've been trying
      > to switch from Perl to Python for administrative tasks - and one thing
      > I cannot understand so far is why I need the special 'self' (or anything
      > esle) argument in class method definitions. I might have missed an
      > explanation in the docs, a quick Google search did not help. Is there
      > somewhere on the web you could direct me to?[/color]

      This entry of the Python FAQ:



      -- Gerhard

      Comment

      • vivek@cs.unipune.ernet.in

        #4
        Re: Why the 'self' argument?

        On Fri, Sep 05, 2003 at 01:58:41PM +0000, Grzegorz Staniak wrote:[color=blue]
        > Hello,
        >
        > I'm a newbie Python user, a systems administrator - I've been trying
        > to switch from Perl to Python for administrative tasks - and one thing
        > I cannot understand so far is why I need the special 'self' (or anything
        > esle) argument in class method definitions. I might have missed an
        > explanation in the docs, a quick Google search did not help. Is there
        > somewhere on the web you could direct me to?
        >
        > Thanks,
        >
        > --
        > Grzegorz Staniak <gstaniak@zagie l.com.pl>
        > --
        > http://mail.python.org/mailman/listinfo/python-list[/color]

        The first argument in a class method is a reference to that class itself. It is
        same as "this" pointer in c++ and java. Using this argument you can access the
        class variables. ex:

        class test:
        def __init__(self,a rg1,arg2): #this acts as class constructor
        self.name=arg1
        self.value=arg2


        def changeName(self ,newName): #here we change the name class variable
        self.name=newNa me
        ..........and so on

        regards
        Vivek Kumar

        Comment

        • John Roth

          #5
          Re: Why the 'self' argument?


          "Grant Edwards" <grante@visi.co m> wrote in message
          news:3f58a2bd$0 $156$a1866201@n ewsreader.visi. com...[color=blue]
          > In article <vlh7otscuo8gb1 @news.supernews .com>, John Roth wrote:
          >[color=green]
          > > Technically, it would be possible to make "self" a reserved
          > > word, and not have to put it in the method declaration.
          > > However, there are a lot of people who use something other than
          > > the word "self," so that would break existing code.[/color]
          >
          > It would also make the language more complex and irregular.[/color]

          How?

          John Roth
          [color=blue]
          >
          > --
          > Grant Edwards grante Yow! Could I have a[/color]
          drug[color=blue]
          > at overdose?
          > visi.com[/color]


          Comment

          • Grant Edwards

            #6
            Re: Why the 'self' argument?

            In article <vlhba85atlte07 @news.supernews .com>, John Roth wrote:[color=blue]
            >
            > "Grant Edwards" <grante@visi.co m> wrote in message
            > news:3f58a2bd$0 $156$a1866201@n ewsreader.visi. com...[color=green]
            >> In article <vlh7otscuo8gb1 @news.supernews .com>, John Roth wrote:
            >>[color=darkred]
            >> > Technically, it would be possible to make "self" a reserved
            >> > word, and not have to put it in the method declaration.
            >> > However, there are a lot of people who use something other than
            >> > the word "self," so that would break existing code.[/color]
            >>
            >> It would also make the language more complex and irregular.[/color]
            >
            > How?[/color]

            1) It would add a reserved word.

            2) It would mean that there's some sort of difference between
            a function and a method.

            --
            Grant Edwards grante Yow! I have a TINY BOWL in
            at my HEAD
            visi.com

            Comment

            • Alex Martelli

              #7
              class methods vs instance methods (was Re: Why the 'self' argument?)

              vivek@cs.unipun e.ernet.in wrote:
              ...[color=blue]
              > The first argument in a class method is a reference to that class itself.[/color]

              Yes, that's what defines CLASS methods (as opposed to ordinary, or
              INSTANCE, methods). But the original poster was not asking about class
              methods, and you're not giving any examples of them -- I only see
              instance methods. I suspect a terminology problem -- you may be using
              the term "class method" in a way that is totally inappropriate to
              Python, where classes are first-level objects.


              So -- in Python, you see...:

              To make a class method you have to call on the classmethod built-in. E.g.:

              class Example(object) :

              def instmeth(self): print 'Instance method of', self

              def clasmeth(cls): print 'Class method of', cls
              clasmeth = classmethod(cla smeth)

              inst = Example()


              Now you can call:

              inst.instmeth()

              or:

              inst.clasmeth()
              Example.clasmet h()

              (both of these do exactly the same thing), but NOT:

              Example.instmet h()

              since an INSTANCE method, differently from a CLASS method,
              needs to be passed the instance as the first argument, either
              implicitly (by CALLING it on the instance) or explicitly
              by passing it when calling the method on the class, as in:

              Example.instmet h(inst)


              Alex

              Comment

              • John Roth

                #8
                Re: Why the 'self' argument?


                "Grant Edwards" <grante@visi.co m> wrote in message
                news:3f58b1f6$0 $155$a1866201@n ewsreader.visi. com...[color=blue]
                > In article <vlhba85atlte07 @news.supernews .com>, John Roth wrote:[color=green]
                > >
                > > "Grant Edwards" <grante@visi.co m> wrote in message
                > > news:3f58a2bd$0 $156$a1866201@n ewsreader.visi. com...[color=darkred]
                > >> In article <vlh7otscuo8gb1 @news.supernews .com>, John Roth wrote:
                > >>
                > >> > Technically, it would be possible to make "self" a reserved
                > >> > word, and not have to put it in the method declaration.
                > >> > However, there are a lot of people who use something other than
                > >> > the word "self," so that would break existing code.
                > >>
                > >> It would also make the language more complex and irregular.[/color]
                > >
                > > How?[/color]
                >
                > 1) It would add a reserved word.[/color]

                So? For most people, self *is* a reserved word anyway. A lot of
                novices think it is. Making it official simplifies things, IMO.
                [color=blue]
                > 2) It would mean that there's some sort of difference between
                > a function and a method.[/color]

                I don't understand your point. There is currently a difference
                between a function and a method that could be eliminated by
                making self a reserved word and removing it from the method
                header. Or have you never tried to invoke a method from the
                wrong context and gotten the "unbound method" error?

                There's no reason why a function in the module space couldn't
                use self to refer to the module. It would simplify things by
                removing much of the need for the global keyword.

                John Roth[color=blue]
                >
                > --
                > Grant Edwards[/color]


                Comment

                • John Roth

                  #9
                  Re: Why the 'self' argument?


                  "Mel Wilson" <mwilson@the-wire.com> wrote in message
                  news:qpOW/ks/KnRV089yn@the-wire.com...[color=blue]
                  > In article <vlhidvcpq9a980 @news.supernews .com>,
                  > "John Roth" <newsgroups@jhr othjr.com> wrote:[color=green]
                  > >"Grant Edwards" <grante@visi.co m> wrote in message
                  > >news:3f58b1f6$ 0$155$a1866201@ newsreader.visi .com...[color=darkred]
                  > >> In article <vlhba85atlte07 @news.supernews .com>, John Roth wrote:
                  > >> >
                  > >> > "Grant Edwards" <grante@visi.co m> wrote in message
                  > >> > news:3f58a2bd$0 $156$a1866201@n ewsreader.visi. com...
                  > >> 2) It would mean that there's some sort of difference between
                  > >> a function and a method.[/color]
                  > >
                  > >I don't understand your point. There is currently a difference
                  > >between a function and a method that could be eliminated by
                  > >making self a reserved word and removing it from the method
                  > >header. Or have you never tried to invoke a method from the
                  > >wrong context and gotten the "unbound method" error?[/color]
                  >
                  > There's no difference in the sense that a method is
                  > simply a function whose first parameter refers to the class
                  > instance to be worked on. No magic words, no "undeclared "
                  > parameters. It means that in my demo code in limitcases.py
                  > (somewhere in the newsgroup lately) I can say
                  >
                  > limitcases.Lowe st.__str__ = lambda x: "-Infinity"
                  >
                  > to give the Lowest class a new method sans ennuis.[/color]

                  But why do it that way? Neither Java nor Ruby require
                  giving the instance a name. Hence my comment that requiring
                  it is more complex than not requiring it.

                  John Roth[color=blue]
                  >
                  > Regards. Mel.[/color]


                  Comment

                  • Michael Peuser

                    #10
                    Re: Why the 'self' argument?


                    "Grzegorz Staniak" <gstaniak@inka. zagiel.pl>
                    [color=blue]
                    > I'm a newbie Python user, a systems administrator - I've been trying
                    > to switch from Perl to Python for administrative tasks - and one thing
                    > I cannot understand so far is why I need the special 'self' (or anything
                    > esle) argument in class method definitions.[/color]

                    You probably do not mean "class methods"; this a technical term in OOP.

                    The Python class basics are *very* similar to the implementation of Perl
                    classes!!
                    You should not have much problems....

                    Kindly
                    Michael P


                    Comment

                    • Grant Edwards

                      #11
                      Re: Why the 'self' argument?

                      In article <vlhtm1bmc7m295 @news.supernews .com>, John Roth wrote:
                      [color=blue][color=green]
                      >> There's no difference in the sense that a method is simply a
                      >> function whose first parameter refers to the class instance to
                      >> be worked on. No magic words, no "undeclared " parameters. It
                      >> means that in my demo code in limitcases.py (somewhere in the
                      >> newsgroup lately) I can say
                      >>
                      >> limitcases.Lowe st.__str__ = lambda x: "-Infinity"
                      >>
                      >> to give the Lowest class a new method sans ennuis.[/color]
                      >
                      > But why do it that way?[/color]

                      So that there's no difference between a function and a method.

                      Simplicity and orthogonality are good things -- despite what
                      C++ proponents thing.
                      [color=blue]
                      > Neither Java nor Ruby require giving the instance a name.[/color]

                      So?

                      Personally I don't like invisible, implied, automatic stuff
                      like that.

                      Can't stand automatic transmissions either -- the damn things
                      have no idea what's ahead, and always seem to be in the wrong
                      gear at critical moments.
                      [color=blue]
                      > Hence my comment that requiring it is more complex than not
                      > requiring it.[/color]

                      No, it's more complex the Java/Ruby way, since you have to have
                      two sets of rules for what a name means depending on whether
                      you're inside a "normal" function or a method. In Python
                      there's just one set of rules -- mostly.

                      I have enough trouble remembering how the stuff I design works. ;)

                      I don't want to have to keep track of more scoping/name-space
                      rules than absolutely necessary.

                      --
                      Grant Edwards grante Yow! Intra-mural sports
                      at results are filtering
                      visi.com through th' plumbing...

                      Comment

                      • Bengt Richter

                        #12
                        Re: Why the 'self' argument?

                        On Fri, 5 Sep 2003 22:51:10 +0200, "Michael Peuser" <mpeuser@web.de > wrote:
                        [color=blue]
                        >
                        >"Grzegorz Staniak" <gstaniak@inka. zagiel.pl>
                        >[color=green]
                        >> I'm a newbie Python user, a systems administrator - I've been trying
                        >> to switch from Perl to Python for administrative tasks - and one thing
                        >> I cannot understand so far is why I need the special 'self' (or anything
                        >> esle) argument in class method definitions.[/color]
                        >
                        >You probably do not mean "class methods"; this a technical term in OOP.
                        >[/color]
                        I suspect the OP meant "class (method definitions)," not "(class method)" definitions." ;-)

                        Re "self," there must be a FAQ entry, but I'm too lazy to look for it.

                        Regards,
                        Bengt Richter

                        Comment

                        • John Roth

                          #13
                          Re: Why the 'self' argument?


                          "Grant Edwards" <grante@visi.co m> wrote in message
                          news:3f59014d$0 $152$a1866201@n ewsreader.visi. com...[color=blue]
                          > In article <vlhtm1bmc7m295 @news.supernews .com>, John Roth wrote:
                          >[color=green][color=darkred]
                          > >> There's no difference in the sense that a method is simply a
                          > >> function whose first parameter refers to the class instance to
                          > >> be worked on. No magic words, no "undeclared " parameters. It
                          > >> means that in my demo code in limitcases.py (somewhere in the
                          > >> newsgroup lately) I can say
                          > >>
                          > >> limitcases.Lowe st.__str__ = lambda x: "-Infinity"
                          > >>
                          > >> to give the Lowest class a new method sans ennuis.[/color]
                          > >
                          > > But why do it that way?[/color]
                          >
                          > So that there's no difference between a function and a method.
                          >
                          > Simplicity and orthogonality are good things -- despite what
                          > C++ proponents thing.
                          >[color=green]
                          > > Neither Java nor Ruby require giving the instance a name.[/color]
                          >
                          > So?
                          >
                          > Personally I don't like invisible, implied, automatic stuff
                          > like that.
                          >
                          > Can't stand automatic transmissions either -- the damn things
                          > have no idea what's ahead, and always seem to be in the wrong
                          > gear at critical moments.
                          >[color=green]
                          > > Hence my comment that requiring it is more complex than not
                          > > requiring it.[/color]
                          >
                          > No, it's more complex the Java/Ruby way, since you have to have
                          > two sets of rules for what a name means depending on whether
                          > you're inside a "normal" function or a method. In Python
                          > there's just one set of rules -- mostly.[/color]

                          As I said earlier, it's quite possible to define it so that there
                          is always an instance of some kind; whether that's an instance
                          a class or the module itself.
                          [color=blue]
                          > I have enough trouble remembering how the stuff I design works. ;)
                          >
                          > I don't want to have to keep track of more scoping/name-space
                          > rules than absolutely necessary.[/color]

                          I think my comments have shown that you can reduce the
                          amount of scoping / name space rules noticably.

                          John Roth[color=blue]
                          >
                          > --
                          > Grant Edwards grante Yow! Intra-mural[/color]
                          sports[color=blue]
                          > at results are filtering
                          > visi.com through th' plumbing...[/color]


                          Comment

                          • John Roth

                            #14
                            Re: Why the 'self' argument?


                            "Michael Peuser" <mpeuser@web.de > wrote in message
                            news:bjat11$70s $07$1@news.t-online.com...[color=blue]
                            >
                            > "Grzegorz Staniak" <gstaniak@inka. zagiel.pl>
                            >[color=green]
                            > > I'm a newbie Python user, a systems administrator - I've been trying
                            > > to switch from Perl to Python for administrative tasks - and one thing
                            > > I cannot understand so far is why I need the special 'self' (or anything
                            > > esle) argument in class method definitions.[/color]
                            >
                            > You probably do not mean "class methods"; this a technical term in OOP.
                            >
                            > The Python class basics are *very* similar to the implementation of Perl
                            > classes!!
                            > You should not have much problems....[/color]

                            Actually, I think he *does* mean "class methods." They are new in 2.2
                            after all, and I missed the qualification the first time around.

                            John Roth[color=blue]
                            >
                            > Kindly
                            > Michael P
                            >
                            >[/color]


                            Comment

                            • Grant Edwards

                              #15
                              Re: Why the 'self' argument?

                              In article <vli387c9mm140a @news.supernews .com>, John Roth wrote:
                              [color=blue][color=green]
                              >> So that there's no difference between a function and a method.
                              >>
                              >> Simplicity and orthogonality are good things -- despite what
                              >> C++ proponents thing.
                              >>[color=darkred]
                              >> > Hence my comment that requiring it is more complex than not
                              >> > requiring it.[/color]
                              >>
                              >> No, it's more complex the Java/Ruby way, since you have to have
                              >> two sets of rules for what a name means depending on whether
                              >> you're inside a "normal" function or a method. In Python
                              >> there's just one set of rules -- mostly.[/color]
                              >
                              > As I said earlier, it's quite possible to define it so that there
                              > is always an instance of some kind; whether that's an instance
                              > a class or the module itself.[/color]

                              I don't follow. You're talking about defining a keyword that
                              always refers to the first parameter passed to a function? And
                              the declared parameters would refer to the 2nd through Nth
                              parameters? What if the keyword isn't used in the function
                              definition, then do the delcared parameters refer to the 1st
                              through Nth?
                              [color=blue]
                              > I think my comments have shown that you can reduce the amount
                              > of scoping / name space rules noticably.[/color]

                              Compared to what? It sure sounds like you're introducing more
                              rules than there are now. How would you propose reducing the
                              number of rules?

                              --
                              Grant Edwards grante Yow! Somewhere in Tenafly,
                              at New Jersey, a chiropractor
                              visi.com is viewing "Leave it to
                              Beaver"!

                              Comment

                              Working...