Newbie question: what's with "self"?

D

donkeyboy

This is probably a really basic question, but anyway ...

I'm new to both Python and OO programming. From looking at a number of
code examples, the word "self" is used a lot when referring to classes.
As such, what does "self" mean and/or do? I've read things that say
it's a naming convention, but no-one has really spelt it out (in idiot
form!) in a way I can understand.

Any help you can provide would be great: at the moment, when code
doesn't work as expected, I'm randomly sprinkling "self"s in all over
the place to see if that helps, but without much of an idea of what it
really achieves.

Thanks in advance!!
 
F

Farshid Lashkari

donkeyboy said:
This is probably a really basic question, but anyway ...

I'm new to both Python and OO programming. From looking at a number of
code examples, the word "self" is used a lot when referring to classes.
As such, what does "self" mean and/or do? I've read things that say
it's a naming convention, but no-one has really spelt it out (in idiot
form!) in a way I can understand.

Any help you can provide would be great: at the moment, when code
doesn't work as expected, I'm randomly sprinkling "self"s in all over
the place to see if that helps, but without much of an idea of what it
really achieves.

Thanks in advance!!

Hi,

Take a look at the following FAQ on the python homepage:

http://www.python.org/infogami-faq/...d-explicitly-in-method-definitions-and-calls/

-Farshid
 
S

Simon Forman

donkeyboy said:
This is probably a really basic question, but anyway ...

I'm new to both Python and OO programming. From looking at a number of
code examples, the word "self" is used a lot when referring to classes.
As such, what does "self" mean and/or do? I've read things that say
it's a naming convention, but no-one has really spelt it out (in idiot
form!) in a way I can understand.

Any help you can provide would be great: at the moment, when code
doesn't work as expected, I'm randomly sprinkling "self"s in all over
the place to see if that helps, but without much of an idea of what it
really achieves.

Thanks in advance!!

When you write a class, it works sort of like a template for creating
instances of that class. The instances have variables (as attributes)
and use the methods (functions with the 'self' parameter) you define in
the class to manipulate the variables.

class foo:

def __init__(self, value=0):
self.value = value

def get(self):
return self.value

def add(self, n):
self.value += n

f = foo() # "magically" calls the __init__ method

# Call some methods on the instance.
print f.get()
f.add(23)
print f.get()

# Access the instance attribute directly.
print f.value
f.value -= 23
print f.value


# The above code prints:
0
23
23
0

Notice how, when you call f.get() and f.add(23), you don't pass in the
'self' parameter? That's because when you call a method (get, add) on
an instance (f) of a class (foo), the python interpreter fills in the
'self' parameter for you as the instance itself. (itSELF, get it?)

Put another way, inside the method, self == f. If you were to create
another instance, g = foo(), and call the methods on/with it, g.get(),
then self == g.

Use can even use the class and pass in the instance yourself,
foo.get(f) and foo.add(f, 23), but there's almost never a reason to do
that.

I hope this helps.


(BTW, don't randomly sprinkle things in your code. Programming ain't
like cooking. You can't just add a dash of 'self's and a sprinkle of
'if..then..'s. :) You should know what you are doing and why. If
not, read and ask questions. Computers are deterministic machines that
do exactly what you tell them to. If you tell them to do random things
you'll get random results, and if *you* don't even understand your code
then it's quite likely no one will and all you'll have is pointless
junk. (And if there's one thing I've learned reading and posting to
c.l.p in the last couple of months, it's that there's *plenty* of
pointless junk around already.) One exception to "don't sprinkle
things" would be print statements, but even there, don't sprinkle
randomly...) :)

Peace,
~Simon
 
T

Thomas Bartkus

donkeyboy said:
This is probably a really basic question, but anyway ...

I'm new to both Python and OO programming. From looking at a number of
code examples, the word "self" is used a lot when referring to classes.
As such, what does "self" mean and/or do? I've read things that say
it's a naming convention, but no-one has really spelt it out (in idiot
form!) in a way I can understand.

Any help you can provide would be great: at the moment, when code
doesn't work as expected, I'm randomly sprinkling "self"s in all over
the place to see if that helps, but without much of an idea of what it
really achieves.

Thanks in advance!!

To put it simply, a class needs a way to know which instance (of itself!) to
operate on.

If you have a class "str" (and Python has that built in!), then there will
be many instances of class "str" in a typical program. The parameter "self"
refers to the particular string the class method is being called to operate
upon.

If you have a method upper() that convert everything to uppercase, your
class definition would need the "self" parameter in order to know which
particular string to convert.

Thomas Bartkus
 
S

skip

donkeyboy> This is probably a really basic question, but anyway ... I'm
donkeyboy> new to both Python and OO programming. From looking at a
donkeyboy> number of code examples, the word "self" is used a lot when
donkeyboy> referring to classes. As such, what does "self" mean and/or
donkeyboy> do?

"self" is a name refering to the particular instance whose method is being
called. If I have

class C:
def __init__(self):
self.x = 7

def printx(self):
print self.x

a = C()
b = C()

If I call a.printx(), self will be bound to the same object a is bound to.
If I call b.printx(), it will be bound to the object b references.

The precise use of the name "self" is indeed simply a convention. You could
call it "barney" if that makes you feel better. ;-)

Skip
 
M

Miki

Hello,
I'm new to both Python and OO programming. From looking at a number of
code examples, the word "self" is used a lot when referring to classes.
As such, what does "self" mean and/or do? I've read things that say
it's a naming convention, but no-one has really spelt it out (in idiot
form!) in a way I can understand.
Note that apart from what all the other pepole said, "self" is not a
reserved word. It's just the wide convention that we use the name
"self".

If you know C++/Java then "self" is like "this".

HTH,
Miki
http://pythonwise.blogspot.com/
 
B

Bruno Desthuilliers

donkeyboy said:
This is probably a really basic question,
but anyway ...

I'm new to both Python and OO programming. From looking at a number of
code examples, the word "self" is used a lot when referring to classes.

Actually, it's used as the first parameter for methods of the classes -
but, due to Python's magic, doesn't need to be passed when calling the
method on an instance.
As such, what does "self" mean and/or do?

It's a reference to the class's instance on which the method was called,
- and it's necessary to have it to know on which instance the method
should act.
I've read things that say
it's a naming convention,

The name 'self' is a convention, yes - you could name it "shruberry" or
"parrot" as well, but it would be rather unusual and somewhat confusing.
FWIW, Python relies a *lot* on naming conventions, and it's better to
stick to them.
but no-one has really spelt it out (in idiot
form!)

Since you ask for it:

<idiot-form>

def reallyspellout(word):
for letter in word:
print "'%s'" % letter,

it = 'self'
reallyspellout(it)

</idiot-form>

Was this idiot enough ?-)

in a way I can understand.

oops, sorry :(

Well, cf above and other answers in this thread then.
Any help you can provide would be great: at the moment, when code
doesn't work as expected, I'm randomly sprinkling "self"s in all over
the place to see if that helps, but without much of an idea of what it
really achieves.

This is known as "accidental programming". A total waste of time and
effort. Read the doc, use google, try to understand, test, and when
something don't work out as expected, ask here. Don't forget the first
three steps.

HTH
 
B

Bruno Desthuilliers

Miki wrote:
(snip)
If you know C++/Java then "self" is like "this".

But in Python, it's mandatory and must be the first arg for instance
methods.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top