[Tutor] Hello and newbie question about "self"
Patrick
python-list at puzzled.xs4all.nl
Mon Feb 4 01:38:52 CET 2008
Hi Alan,
Alan Gauld wrote:
> "Patrick" <python-list at puzzled.xs4all.nl> wrote
>
>> Can anyone please point me to a document that explains "self" in
>> layman's terms.
>
> Try the OOP topic inmy tutorial...
Thanks will have a look.
>> Or lacking such a doc throw in a much appreciated
>> layman's explanation what "self" is and when/where to use it?
>
> Others have given code samples but a conceptuial explanation
> is that
> a) self is only used in OO programming within metjhods of a class.
Now that really helps. I was wondering about that and this answers it.
> b) self refers to the actual instance of the object receiving the
> message with caused the method to be invoked.
This and reading chapter 23 in the book makes things much clearer now.
Thanks!
> Thus if we have a class C with a method m and 3 instances
> a,b and z then when we invoke a.m() self will refer to a and
> when we invoke b.m() self will refer to b. This means that the
> innards of the method can use self to access the instance
> specific data for that invocation.
Even more clear now :)
> If you have used C++ at all you might recognise it as the
> same as 'this' in C++ except that in Python you must explicitly
> specify it whereas C++ creates 'this' magically behind the scenes.
Last time I used C++ was (iirc) in 1987 with a Borland product. I recall
"this" and remember I got stuck on it then too.
> See my tutorial for more on this under the heading
> "Using classes".
Will do. Thanks for the pointer.
> If you haven't started writing classes yet, you can safely ignore
> it for now!
I probably won't need to start writing classes but I really want to
finish the book before I start coding something. I have a small script I
did in (horrible) bash and look forward to try to implement it in (less
horrible) Python.
Thanks for your help.
Regards,
Patrick
More information about the Tutor
mailing list