Why the 'self' argument?

Discussion in 'Python' started by Grzegorz Staniak, Sep 5, 2003.

  1. 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 <>
     
    Grzegorz Staniak, Sep 5, 2003
    #1
    1. Advertising

  2. Grzegorz Staniak wrote:
    > 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?


    This entry of the Python FAQ:

    http://www.python.org/doc/faq/gener...ed-explicitly-in-method-definitions-and-calls

    -- Gerhard
     
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Sep 5, 2003
    #2
    1. Advertising

  3. Grzegorz Staniak

    Guest

    On Fri, Sep 05, 2003 at 01:58:41PM +0000, Grzegorz Staniak wrote:
    > 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 <>
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    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,arg1,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=newName
    ..........and so on

    regards
    Vivek Kumar
     
    , Sep 5, 2003
    #3
  4. Lukasz Pankowski, Sep 5, 2003
    #4
  5. In article <>, 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.

    --
    Grant Edwards grante Yow! Could I have a drug
    at overdose?
    visi.com
     
    Grant Edwards, Sep 5, 2003
    #5
  6. Grzegorz Staniak

    John Roth Guest

    "Grant Edwards" <> wrote in message
    news:3f58a2bd$0$156$...
    > In article <>, 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.


    How?

    John Roth

    >
    > --
    > Grant Edwards grante Yow! Could I have a

    drug
    > at overdose?
    > visi.com
     
    John Roth, Sep 5, 2003
    #6
  7. In article <>, John Roth wrote:
    >
    > "Grant Edwards" <> wrote in message
    > news:3f58a2bd$0$156$...
    >> In article <>, 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.

    >
    > How?


    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
     
    Grant Edwards, Sep 5, 2003
    #7
  8. class methods vs instance methods (was Re: Why the 'self' argument?)

    wrote:
    ...
    > The first argument in a class method is a reference to that class itself.


    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(clasmeth)

    inst = Example()


    Now you can call:

    inst.instmeth()

    or:

    inst.clasmeth()
    Example.clasmeth()

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

    Example.instmeth()

    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.instmeth(inst)


    Alex
     
    Alex Martelli, Sep 5, 2003
    #8
  9. Grzegorz Staniak

    John Roth Guest

    "Grant Edwards" <> wrote in message
    news:3f58b1f6$0$155$...
    > In article <>, John Roth wrote:
    > >
    > > "Grant Edwards" <> wrote in message
    > > news:3f58a2bd$0$156$...
    > >> In article <>, 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.

    > >
    > > How?

    >
    > 1) It would add a reserved word.


    So? For most people, self *is* a reserved word anyway. A lot of
    novices think it is. Making it official simplifies things, IMO.

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


    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
    >
    > --
    > Grant Edwards
     
    John Roth, Sep 5, 2003
    #9
  10. Grzegorz Staniak

    John Roth Guest

    "Mel Wilson" <> wrote in message
    news:qpOW/ks/...
    > In article <>,
    > "John Roth" <> wrote:
    > >"Grant Edwards" <> wrote in message
    > >news:3f58b1f6$0$155$...
    > >> In article <>, John Roth wrote:
    > >> >
    > >> > "Grant Edwards" <> wrote in message
    > >> > news:3f58a2bd$0$156$...
    > >> 2) It would mean that there's some sort of difference between
    > >> a function and a method.

    > >
    > >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 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.Lowest.__str__ = lambda x: "-Infinity"
    >
    > to give the Lowest class a new method sans ennuis.


    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
    >
    > Regards. Mel.
     
    John Roth, Sep 5, 2003
    #10
  11. "Grzegorz Staniak" <>

    > 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.


    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
     
    Michael Peuser, Sep 5, 2003
    #11
  12. In article <>, John Roth wrote:

    >> 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.Lowest.__str__ = lambda x: "-Infinity"
    >>
    >> to give the Lowest class a new method sans ennuis.

    >
    > But why do it that way?


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

    Simplicity and orthogonality are good things -- despite what
    C++ proponents thing.

    > Neither Java nor Ruby require giving the instance a name.


    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.

    > Hence my comment that requiring it is more complex than not
    > requiring it.


    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...
     
    Grant Edwards, Sep 5, 2003
    #12
  13. On Fri, 5 Sep 2003 22:51:10 +0200, "Michael Peuser" <> wrote:

    >
    >"Grzegorz Staniak" <>
    >
    >> 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.

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

    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
     
    Bengt Richter, Sep 5, 2003
    #13
  14. Grzegorz Staniak

    John Roth Guest

    "Grant Edwards" <> wrote in message
    news:3f59014d$0$152$...
    > In article <>, John Roth wrote:
    >
    > >> 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.Lowest.__str__ = lambda x: "-Infinity"
    > >>
    > >> to give the Lowest class a new method sans ennuis.

    > >
    > > But why do it that way?

    >
    > So that there's no difference between a function and a method.
    >
    > Simplicity and orthogonality are good things -- despite what
    > C++ proponents thing.
    >
    > > Neither Java nor Ruby require giving the instance a name.

    >
    > 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.
    >
    > > Hence my comment that requiring it is more complex than not
    > > requiring it.

    >
    > 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.


    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.

    > 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.


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

    John Roth
    >
    > --
    > Grant Edwards grante Yow! Intra-mural

    sports
    > at results are filtering
    > visi.com through th' plumbing...
     
    John Roth, Sep 5, 2003
    #14
  15. Grzegorz Staniak

    John Roth Guest

    "Michael Peuser" <> wrote in message
    news:bjat11$70s$07$-online.com...
    >
    > "Grzegorz Staniak" <>
    >
    > > 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.

    >
    > 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....


    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
    >
    > Kindly
    > Michael P
    >
    >
     
    John Roth, Sep 5, 2003
    #15
  16. In article <>, John Roth wrote:

    >> So that there's no difference between a function and a method.
    >>
    >> Simplicity and orthogonality are good things -- despite what
    >> C++ proponents thing.
    >>
    >> > Hence my comment that requiring it is more complex than not
    >> > requiring it.

    >>
    >> 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.

    >
    > 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.


    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?

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


    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"!
     
    Grant Edwards, Sep 6, 2003
    #16
  17. Grzegorz Staniak

    John Roth Guest

    "Grant Edwards" <> wrote in message
    news:3f591d28$0$175$...
    > In article <>, John Roth wrote:
    >
    > >> So that there's no difference between a function and a method.
    > >>
    > >> Simplicity and orthogonality are good things -- despite what
    > >> C++ proponents thing.
    > >>
    > >> > Hence my comment that requiring it is more complex than not
    > >> > requiring it.
    > >>
    > >> 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.

    > >
    > > 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.

    >
    > 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?


    When Python invokes a function, it binds the operands in the
    function call to the identifiers named in the function / method
    header. If this is a method, it binds the instance (which is not
    in the invocation parameter list, btw.) to the first
    parameter in the method header.

    If you make "self" a reserved word, then all it has to do
    is bind the instance to "self," rather than the first identifier
    in the parameter list.

    In current python, there are two classes of functions (module
    level and embedded) and three classes of methods (instance,
    class and static.) Instance methods get the current instance,
    class methods get the class object as the instance, and the other
    three categories get nothing.

    As a separate suggestion, I'd have Python bind the module
    object to "self" for module functions and static methods. I
    haven't figured out what I want done with embedded methods
    and unbound methods yet. Notice that this eliminates the
    need for the global statement for module functions - all
    identifiers are accessible for rebinding through "self."

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

    >
    > 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?


    If you don't have to write "self" as the first parameter of a method,
    that reduces the complexity. Everything else remains the same.

    John Roth
    >
    > --
    > Grant Edwards grante Yow! Somewhere in

    Tenafly,
    > at New Jersey, a

    chiropractor
    > visi.com is viewing "Leave it to
    > Beaver"!
     
    John Roth, Sep 6, 2003
    #17
  18. On Fri, 5 Sep 2003 20:15:00 -0400, "John Roth" <> wrote:
    [...]

    >If you don't have to write "self" as the first parameter of a method,
    >that reduces the complexity. Everything else remains the same.
    >

    Except for cases where two self-parameters are visible at the same time,
    and both are used in an inner scope and must be distinguished by giving
    them different names. ISTM you'd have to turn off some dynamic capabilities
    of python to make "self" implicit. (An example came up in a past discussion,
    some time ago).

    Regards,
    Bengt Richter
     
    Bengt Richter, Sep 6, 2003
    #18
  19. Grzegorz Staniak

    John Roth Guest

    "Bengt Richter" <> wrote in message
    news:bjbb57$bb2$0@216.39.172.122...
    > On Fri, 5 Sep 2003 20:15:00 -0400, "John Roth" <>

    wrote:
    > [...]
    >
    > >If you don't have to write "self" as the first parameter of a method,
    > >that reduces the complexity. Everything else remains the same.
    > >

    > Except for cases where two self-parameters are visible at the same time,
    > and both are used in an inner scope and must be distinguished by giving
    > them different names. ISTM you'd have to turn off some dynamic

    capabilities
    > of python to make "self" implicit. (An example came up in a past

    discussion,
    > some time ago).


    You could still give them different names. The proposal is to eliminate
    the need to specify "self" in the method header, not in the code in the
    method body. That's a different issue, and I don't know of any language
    that manages to do it consistently.

    John Roth
    >
    > Regards,
    > Bengt Richter
     
    John Roth, Sep 6, 2003
    #19
  20. Grzegorz Staniak

    Paul Foley Guest

    On 05 Sep 2003 15:55:34 GMT, Grant Edwards wrote:

    > In article <>, John Roth wrote:
    >>
    >> "Grant Edwards" <> wrote in message
    >> news:3f58a2bd$0$156$...
    >>> In article <>, 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.

    >>
    >> How?


    > 1) It would add a reserved word.


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


    There *is* a difference between a function and a method (a method
    /contains/ a function), unless you just mean that they're both
    callable (which is not necessarily true in other languages)

    But you can't define methods, you can only define functions (Python
    makes methods by "magic"), so the "self" parameter is no different
    than any other...it's pretty weird to suggest that it should be
    treated differently. Why not treat the 5th parameter specially?

    --
    Just because we Lisp programmers are better than everyone else is no
    excuse for us to be arrogant. -- Erann Gat

    (setq reply-to
    (concatenate 'string "Paul Foley " "<mycroft" '(#\@) "actrix.gen.nz>"))
     
    Paul Foley, Sep 6, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    586
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    599
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    404
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    379
    falcon
    Jul 31, 2005
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,992
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page