-1

So, I don't understand how 'self' is never defined, but it's always used.

Ex:

 def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,"My Frame",size=(300,200))
        panel=wx.Panel(self)

        self=self.CreateStatusBar()
        menu=CreateMenuBar()
        first=wx.Menu()
        first.Append(NewId(),"Close window","Yup")
        menu.Append(first,"File")

I'm not sure if this is correct, and I'm still working out the bugs, and this is just part of the script, but howcome you don't define self? You can just go self.CreateStatusBar(). How does Python know what self is? Please explain in simple words.

Thanks! :)

jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • 5
    http://stackoverflow.com/questions/2709821/python-self-explained – Kyle C Feb 22 '12 at 20:36
  • In Python, nothing is defined. I'm not sure what you're asking. `self=self.CreateStatusBar()` is unlikely to be correct, also. Where did you see this code? – S.Lott Feb 22 '12 at 20:36
  • Interestingly, self is not a special keyword at all. You could use any word in place of it. But please don't. You're code would be difficult to read by others if you used anything except self. – Kris Harper Feb 22 '12 at 20:37

5 Answers5

2

self in python refers to the instance variable. It is a reference to the current object. It is equivalent to "this" in other languages, but in Python, "explicit is better than implicit", so it is actually listed as the first argument in any function (in essence being defined there).

(Note: it doesn't actually have to be called self. You can name it anything you want. However, this is the common convention.)

S.Lott
  • 384,516
  • 81
  • 508
  • 779
froadie
  • 79,995
  • 75
  • 166
  • 235
2

self means the object that you are calling the method on. So this code:

class Foo(object):
    ...
    def bar(self, msg):
        print msg

f = Foo()
f.bar("hello")

is equivalent to:

f = Foo()
Foo.bar(f, "hello")
snim2
  • 4,004
  • 27
  • 44
1

self is a reference to the object itself. It's like this in C++ or Java. It's a calling convention, meaning that you don't set it, but the language figures it out (just like you never actually set this in other languages, yet somehow it's always there).

o = SomeClass()
o.someMethod('hi')

The above code could just as easily be written as:

o = SomeClass()
SomeClass.someMethod(o, 'hi')

The first parameter (self) is the object itself.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • Allow me to add that unlike other languages, it _is_ defined as the first argument to anmethod, it is just automatically filled up by Python. In other OO language, themagic word "this" works teh same as Python's "self" but it is indeed not defined anywhere. – jsbueno Feb 22 '12 at 20:37
  • Ohhh! So by saying "self.blah" your saying "this current object"? – Fireflame09225 Feb 22 '12 at 20:50
  • @Fireflame09225 - right. Or more accurately, you're saying "this current object's blah" :) – froadie Feb 22 '12 at 20:54
1

Self is used to denote that you are referring to something in the current instance of a class i.e the current object you are dealing with. You don't need to define it, because it is automatically passed in to instance methods .

If you're not familiar with objects, check out this link that explains this vocab in simple terms.

http://www.tutorialspoint.com/python/python_classes_objects.htm

NPix
  • 121
  • 3
0

Try defining a member function for a class, but forget to include self:

>>> class Foo:
        def func(param):
            print param

>>> f = Foo() #make an object of type Foo

When you call func you'll get an error:

>>> f.func("Hello") # call a method on the Foo object

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    f.func("Hello")
TypeError: func() takes exactly 1 argument (2 given)

The error is saying that two parameters were passed to the function, even though we only passed "Hello". Python implicitly passes the object referenced by f to the function func defined by class Foo, as the first parameter, which by convention we name self.

>>> class Foo:
        def func(self, param):
            print self, param

>>> f = Foo() #make an object of type Foo
>>> f.func("Hello") # call a method on the Foo object
<__main__.Foo instance at 0xb6b6ddcc> Hello

>>> f
<__main__.Foo instance at 0xb6b6ddcc>

This tells us that self is an instance of Foo, and is the same instance as f.

Peter Wood
  • 23,859
  • 5
  • 60
  • 99