- what is self variable? 
Author Message
 - what is self variable?

hello,

i've already written a few python scripts but am just learning oop. i notice
many examples make use of a 'self' variable under __init__ , what does this
referr to?

is it like a this pointer?

thanks,

Cervantes Vive



Sun, 16 Nov 2003 23:15:19 GMT  
 - what is self variable?

Quote:
> i've already written a few python scripts but am just learning
> oop. i notice many examples make use of a 'self' variable
> under __init__ , what does this referr to?

> is it like a this pointer?

it is very much like the C "this" pointer. actually, the
name isn't special in anyway, you could use "this" if you
wanted, but i've yet to see any code that uses something
besides "self".

unlike C, you _must_ use this self variable when referencing
members and methods for the class.

this "self" is always the first argument passed to any
method functions of a class. when you "forget" to include
it in the method argument list, you'll quickly get
"wrong number of argument" errors when you try to call
the method.



Sun, 16 Nov 2003 23:36:30 GMT  
 - what is self variable?

Quote:

> i've already written a few python scripts but am just learning oop. i
> notice
> many examples make use of a 'self' variable under __init__ , what does
> this
> referr to?

> is it like a this pointer?

In a word, yes. See <http://www.python.org/doc/current/tut/node11.html> for
details.

Cheers,
Simon Brunning
TriSystems Ltd.

-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.



Sun, 16 Nov 2003 23:28:05 GMT  
 - what is self variable?
| hello,
|
| i've already written a few python scripts but am just learning oop. i notice
| many examples make use of a 'self' variable under __init__ , what does this
| referr to?
|
| is it like a this pointer?

Yes.  Instead of being passed to the function implicitly (C++, Java)
it is passed explicitly as the first argument.  It is conventionally
called 'self', though it can be called anything you want to call it
(including 'this').

-D



Sun, 16 Nov 2003 23:31:38 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Self-fetching variables

2. Accessing self defined global variables within a function

3. self-reproducing Prolog program With Variables

4. Saving a Variable to a field in a record- what am i doing wrong

5. Self-reproduction or self-changing of Web pages

6. Self-reproduction or self-change of Web page

7. Self-reproduction or self-changing of Web pages

8. self-replicating-code, self-replicating-messages

9. I am not deaf, but am I mute?

10. UserList.__getslice__(): copy.copy(self.data) vs. self.__class__(self.data).

11. Class variables / global variables / Init variables

12. J and variable variables

 

 
Powered by phpBB® Forum Software