9

Possible Duplicate:
Python 'self' keyword

Forgive me if this is an incredibly noobish question, but I never did understand self in Python. What does it do? And when I see things like

def example(self, args):
    return self.something

what do they do? I think I've seen args somewhere in a function too. Please explain in a simple way :P

Community
  • 1
  • 1
  • 5
    Please read http://docs.python.org/tutorial/classes.html as it should prove to be more useful than any answer (me think) –  Jul 14 '11 at 18:19
  • Please search stack overflow for "python self". http://stackoverflow.com/search?q=python+self. Then, after reading all the related questions, consider closing this one unless it is someone unique. – S.Lott Jul 14 '11 at 18:21
  • 2
    @sixfeetsix - who learns a language by reading the documentation front to back before writing any code? We can be more helpful than that. – Kenan Banks Jul 14 '11 at 18:27
  • 2
    I don't agree. Lots of questions asked on this site have easy answers in the docs. –  Jul 14 '11 at 18:29
  • 1
    @sixfeetsix I find the docs overwhelming, that's why I came here. –  Jul 14 '11 at 18:37
  • 2
    I'm not saying that's your question is bad, far from it, but I strongly believe it's better to point to documentation for simple enough stuff. Perhaps this site should have a self-close option "I found what I was looking for it in the docs; here is the link". In any case, I find it silly to have X people scramble to be the first in giving some answer to such simple questions since the OP will often end up with imprecise, incomplete, or confusing information all the while there exists some perfectly fine professionally maintained docs available online and easily searchable from search engines. –  Jul 14 '11 at 18:46
  • "perfectly fine professionally maintained docs" are great for people who are already professional programmers, like me and, I'm guessing you. It should be obvious to any pro that the OP is a total newbie. Pointing them to docs that, whether you realize it or not, require a TON of pre-existing knowledge to be comprehensible, is not helpful. – Kenan Banks Jul 14 '11 at 19:25
  • I get your point, and I'm inclined to agree with you say 50%, _but_ you still don't address the huge waste of time encountered the "newbies" out there just because they can take forever to learn how valuable it is to _try_ and eventually _succeed_ at understanding docs, especially with python docs which tend to be very easy to follow, unlike say say, an RFC about NTP. –  Jul 14 '11 at 19:35
  • "should be obvious to any pro that the OP is a total newbie". That's far from true. It's not obvious. The OP could be lazy rather than a n00b. – S.Lott Jul 14 '11 at 20:05
  • 1
    I want to insist that I generally don't see anything wrong with questions that are better answered by the docs; also I understand that "newbies" and not so "newbies" appreciate that it's ok to ask pretty much anything (within the established guidelines). In my opinion the guidelines should encourage, when it makes sense like in this case, reading docs/standards/specifications/etc, and if the OP still doesn't understand, he can say so and then the community will be _way_ more helpful in providing examples/clarifications/etc. –  Jul 14 '11 at 20:22

3 Answers3

14

It sounds like you've stumbled onto the object oriented features of Python.

self is a reference to an object. It's very close to the concept of this in many C-style languages. Check out this code:

class Car(object):

  def __init__(self, make):

      # Set the user-defined 'make' property on the self object 
      self.make = make

      # Set the 'horn' property on the 'self' object to 'BEEEEEP'
      self.horn = 'BEEEEEP'

  def honk(self):

      # Now we can make some noise!
      print self.horn

# Create a new object of type Car, and attach it to the name `lambo`. 
# `lambo` in the code below refers to the exact same object as 'self' in the code above.

lambo = Car('Lamborghini')
print lambo.make
lambo.honk()
Kenan Banks
  • 207,056
  • 34
  • 155
  • 173
  • That code gives an error `File "", line 20, in AttributeError: 'Car' object has no attribute 'name' ` –  Jul 14 '11 at 18:33
  • Figured it out, thanks guys. So basically, if say there's a variable in a class (not a nested function), I can access it using `self`? –  Jul 14 '11 at 18:44
  • Fixed the error, Fike. And yes that's correct if you're saying what I think you are. A "variable in a class" is technically a "property on an object", but if you play around with the example you'll start to get the hang of it... – Kenan Banks Jul 14 '11 at 19:22
6

self is the reference to the instance of the class that the method (the example function in this case) is of.

You'll want to take a look at the Python docs on the class system for a full introduction to Python's class system. You'll also want to look at these answers to other questions about the subject on Stackoverflow.

Community
  • 1
  • 1
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
3

Self it a reference to the instance of the current class. In your example, self.something references the something property of the example class object.

George Cummins
  • 28,485
  • 8
  • 71
  • 90