Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Why the 'self' argument? (http://www.velocityreviews.com/forums/t322099-why-the-self-argument.html)

Grzegorz Staniak 09-05-2003 01:58 PM

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@zagiel.com.pl>

=?ISO-8859-1?Q?Gerhard_H=E4ring?= 09-05-2003 02:13 PM

Re: Why the 'self' argument?
 
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


vivek@cs.unipune.ernet.in 09-05-2003 02:22 PM

Re: Why the 'self' argument?
 
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 <gstaniak@zagiel.com.pl>
> --
> 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


Lukasz Pankowski 09-05-2003 02:29 PM

Re: Why the 'self' argument?
 
Hi,

the answer is in python FAQ:

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

--

=*= Lukasz Pankowski =*=

Grant Edwards 09-05-2003 02:50 PM

Re: Why the 'self' argument?
 
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.

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

John Roth 09-05-2003 03:35 PM

Re: Why the 'self' argument?
 

"Grant Edwards" <grante@visi.com> wrote in message
news:3f58a2bd$0$156$a1866201@newsreader.visi.com.. .
> 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.


How?

John Roth

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

drug
> at overdose?
> visi.com




Grant Edwards 09-05-2003 03:55 PM

Re: Why the 'self' argument?
 
In article <vlhba85atlte07@news.supernews.com>, John Roth wrote:
>
> "Grant Edwards" <grante@visi.com> wrote in message
> news:3f58a2bd$0$156$a1866201@newsreader.visi.com.. .
>> 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.

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

Alex Martelli 09-05-2003 04:12 PM

class methods vs instance methods (was Re: Why the 'self' argument?)
 
vivek@cs.unipune.ernet.in 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


John Roth 09-05-2003 05:36 PM

Re: Why the 'self' argument?
 

"Grant Edwards" <grante@visi.com> wrote in message
news:3f58b1f6$0$155$a1866201@newsreader.visi.com.. .
> In article <vlhba85atlte07@news.supernews.com>, John Roth wrote:
> >
> > "Grant Edwards" <grante@visi.com> wrote in message
> > news:3f58a2bd$0$156$a1866201@newsreader.visi.com.. .
> >> 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.

> >
> > 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 09-05-2003 08:48 PM

Re: Why the 'self' argument?
 

"Mel Wilson" <mwilson@the-wire.com> wrote in message
news:qpOW/ks/KnRV089yn@the-wire.com...
> In article <vlhidvcpq9a980@news.supernews.com>,
> "John Roth" <newsgroups@jhrothjr.com> wrote:
> >"Grant Edwards" <grante@visi.com> wrote in message
> >news:3f58b1f6$0$155$a1866201@newsreader.visi.com. ..
> >> In article <vlhba85atlte07@news.supernews.com>, John Roth wrote:
> >> >
> >> > "Grant Edwards" <grante@visi.com> wrote in message
> >> > news:3f58a2bd$0$156$a1866201@newsreader.visi.com.. .
> >> 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.





All times are GMT. The time now is 08:54 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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