What is the "self" name always referring to...?

T

Todd Gardner

Pardon my extremely ignorant newbie questions.

Where can I go to find more information about the "self" argument?

Is there something special about the word "self" or did Mr. Guido van
Rossum just decide to us the word arbitrarily?

More precisely it seems that most all classes have "self" as the first
parameter. This may be directly obvious but is the word "self" always
referring to the class or the function it is used in?

Is it a reserved word?

Thank you in advance,

Todd
 
F

Fredrik Lundh

Todd said:
Pardon my extremely ignorant newbie questions.

Where can I go to find more information about the "self" argument?

Is there something special about the word "self" or did Mr. Guido van
Rossum just decide to us the word arbitrarily?

More precisely it seems that most all classes have "self" as the first
parameter. This may be directly obvious but is the word "self" always
referring to the class or the function it is used in?

self is used in *methods* (which are functions associated with
classes), and is used to refer to the current instance of the class
that method belongs to.

for more on classes, see the tutorial:

http://www.python.org/doc/current/tut/node11.html

the FAQ also contains some helpful entries:

http://www.python.org/doc/faq/programming.html#what-is-self
http://www.python.org/doc/faq/gener...ed-explicitly-in-method-definitions-and-calls
Is it a reserved word?

nope, but it's usually a good idea to treat it as if it were.

</F>
 
J

John E. Barham

Todd Gardner said:
Where can I go to find more information about the "self" argument?

From the Python tutorial:

"Conventionally, the first argument of methods is often called self. This is
nothing more than a convention: the name self has absolutely no special
meaning to Python. (Note, however, that by not following the convention your
code may be less readable by other Python programmers, and it is also
conceivable that a class browser program be written which relies upon such a
convention.) "

http://www.python.org/doc/current/tut/node11.html#SECTION0011400000000000000000
Is there something special about the word "self" or did Mr. Guido van
Rossum just decide to us the word arbitrarily?

You could use anything, but the convention for using "self" is very strong.
More precisely it seems that most all classes have "self" as the first
parameter. This may be directly obvious but is the word "self" always
referring to the class or the function it is used in?

I'm not sure what you're asking here. The first argument of any class
method (i.e., "self" per the above discussion) is the object on which the
method is being called. It's equivalent to the "this" keyword in C++
(Java?), although in C++ "this" is implied and is not part of the argument
list.

A contrived example:

C++:

class Foo {
public:
Foo(int a)
{
a_ = a;
this->a_ = a; // same as above
}

int getA() { return a_; }

private:
int a_;
};

Python:

class Foo:
def __init__(self, a):
self._a = a

def getA(self):
return self._a

Is it a reserved word?
No.

Thank you in advance,

Sure.

John
 
M

Mark McEahern

Is there something special about the word "self" or did Mr. Guido van
Rossum just decide to us the word arbitrarily?

Yes, there is something special about "self": It's what most people use
when writing Python to refer to the current instance within a method of
a class.

You can spell "the current instance" however you want:

class Foo:

def __init__(iLikeToBeARebelWithoutACause):
iLikeToBeARebelWithoutACause.bar = True

f = Foo()
print f.bar

But *should* you? <wink>

Cheers,

// m
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top