473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
3 2408
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Fernando M. | last post by:
Hi, i was just wondering about the need to put "self" as the first parameter in every method a class has because, if it's always needed, why the obligation to write it? couldn't it be implicit?...
20
by: Wayne Sutton | last post by:
OK, I'm a newbie... I'm trying to learn Python & have had fun with it so far. But I'm having trouble following the many code examples with the object "self." Can someone explain this usage in...
9
by: shannonl | last post by:
Hi all, For some reason this bind is calling the donothing function, like it should, but is then allowing the text to be inserted into the Text widget. Here is the code: ...
13
by: Kurda Yon | last post by:
Hi, I found one example which defines the addition of two vectors as a method of a class. It looks like that: class Vector: def __add__(self, other): data = for j in range(len(self.data)):...
8
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.