By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Todd Gardner" wrote:
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/tu...00000000000000
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
Jul 18 '05 #2

P: n/a
Todd Gardner wrote:
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/progra...l#what-is-self
http://www.python.org/doc/faq/genera...ions-and-calls
Is it a reserved word?


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

</F>


Jul 18 '05 #3

P: n/a
On Wed, 2003-12-10 at 00:31, Todd Gardner wrote:
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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.