Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why the 'self' argument?

Reply
Thread Tools

Why the 'self' argument?

 
 
Grzegorz Staniak
Guest
Posts: n/a
 
      09-05-2003
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 <>
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Gerhard_H=E4ring?=
Guest
Posts: n/a
 
      09-05-2003
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/genera...ions-and-calls

-- Gerhard

 
Reply With Quote
 
 
 
 
vivek@cs.unipune.ernet.in
Guest
Posts: n/a
 
      09-05-2003
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

 
Reply With Quote
 
Lukasz Pankowski
Guest
Posts: n/a
 
      09-05-2003
Hi,

the answer is in python FAQ:

http://python.org/doc/faq/general.ht...ions-and-calls

--

=*= Lukasz Pankowski =*=
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      09-05-2003
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
 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      09-05-2003

"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



 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      09-05-2003
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
 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      09-05-2003
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

 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      09-05-2003

"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



 
Reply With Quote
 
John Roth
Guest
Posts: n/a
 
      09-05-2003

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57