39

I'm pretty much ignorant of OOP jargon and concepts. I know conceptually what an object is, and that objects have methods. I even understand that in python, classes are objects! That's cool, I just don't know what it means. It isn't clicking with me.

I'm currently trying to understand a few detailed answers that I think will illuminate my understanding of python:

  1. What does the "yield" keyword do in Python?
  2. What is a metaclass in Python?

In the first answer, the author uses the following code as an example:

>>> class Bank(): # let's create a bank, building ATMs
...    crisis = False
...    def create_atm(self) :
...        while not self.crisis :
...            yield "$100"

I don't immediately grok what self is pointing to. This is definitely a symptom of not understanding classes, which I will work on at some point. To clarify, in

>>> def func():
...   for i in range(3):
...     print i

I understand that i points to an item in the list range(3) which, since it is in a function, isn't global. But what does self "point to"?

martineau
  • 119,623
  • 25
  • 170
  • 301
jrhorn424
  • 1,981
  • 2
  • 21
  • 27
  • 9
    self is not a keyword... – JBernardo Aug 09 '11 at 00:21
  • 4
    the bank example is confusing because "crisis" is a class attribute, so the 4th line might have been better written `while not Bank.crisis:`. what's happening is that self is pointing to the object, but since the object doesn't have a "crisis" attribute python then looks at the class. that's how you manage to "see" the crisis attribute, even though self is pointing to the object. – andrew cooke Aug 09 '11 at 00:35

8 Answers8

98

I'll try to clear up some confusion about classes and objects for you first. Lets look at this block of code:

>>> class Bank(): # let's create a bank, building ATMs
...    crisis = False
...    def create_atm(self) :
...        while not self.crisis :
...            yield "$100"

The comment there is a bit deceptive. The above code does not "create" a bank. It defines what a bank is. A bank is something which has a property called crisis, and a function create_atm. That's what the above code says.

Now let's actually create a bank:

>>> x = Bank()

There, x is now a bank. x has a property crisis and a function create_atm. Calling x.create_atm(); in python is the same as calling Bank.create_atm(x);, so now self refers to x. If you add another bank called y, calling y.create_atm() will know to look at y's value of crisis, not x's since in that function self refers to y.

self is just a naming convention, but it is very good to stick with it. It's still worth pointing out that the code above is equivalent to:

>>> class Bank(): # let's create a bank, building ATMs
...    crisis = False
...    def create_atm(thisbank) :
...        while not thisbank.crisis :
...            yield "$100"
Paul
  • 139,544
  • 27
  • 275
  • 264
  • 18
    Actually, while beginners shouldn't know or care, classes ARE objects (of type 'type') in Python. ;) – Russell Borogove Aug 09 '11 at 00:45
  • 1
    +1 for great insight on the comment. This did clear up confusion on classes. I hadn't fully appreciated the difference between a class and an instance, and didn't know you could even create an object by `Class.method(obj)`. – jrhorn424 Aug 09 '11 at 15:42
  • 1
    Accepted since, when I read this answer, things started clicking. Other answers were essential to understanding, as well! – jrhorn424 Aug 10 '11 at 15:29
  • 1
    Isn't this wrong? Looking at this example: http://stackoverflow.com/a/475873/3402768 . Because crisis doesn't use `self` in the initialization it is a class variable and is shared between instances. So bank x and y check on the same value of crisis?! – uitty400 Aug 19 '16 at 09:31
  • I checked. Both examples are correct. But then I do not understand the difference. – uitty400 Aug 19 '16 at 10:50
  • apparently an often asked question. Get your answer here for example: http://stackoverflow.com/questions/39039884/class-variables-class-list-vs-class-boolean?noredirect=1#39040051 – uitty400 Aug 19 '16 at 14:24
  • 1
    `x.create_atm(); in python is the same as calling Bank.create_atm(x);` I find this a very helpful analogy actually – Mote Zart Mar 28 '19 at 23:54
19

It may help you to think of the obj.method(arg1, arg2) invocation syntax as purely syntactic sugar for calling method(obj, arg1, arg2) (except that method is looked up via obj's type, and isn't global).

If you view it that way, obj is the first argument to the function, which traditionally is named self in the parameter list. (You can, in fact, name it something else, and your code will work correctly, but other Python coders will frown at you.)

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 1
    +1. This view also explains the `foo() takes exactly n arguments, n+1 given` When the caller is passing `n` arguments, but should really be passing `n-1` arguments – John La Rooy Aug 09 '11 at 04:07
  • 2
    I would have said think of it as sugar for `type(obj).method(obj, arg1, arg2)` which avoids the wooliness over the lookup. – Duncan Aug 09 '11 at 08:46
  • So, @Chris, this means that every time I define a function in my class, if I want that function to act on an instance of that class, I need to have one mandatory argument called `self`? – jrhorn424 Aug 09 '11 at 15:40
  • 3
    @jrhorn424: Yes, and it must be the first parameter. (And if you _don't_ want to use an instance of the class---if it's a static method---then, in order to avoid having to specify the (unused) `self` parameter, you must decorate that method with the [`@staticmethod`](http://docs.python.org/library/functions.html#staticmethod) decorator.) – C. K. Young Aug 09 '11 at 17:20
14

"self" is the instance object automatically passed to the class instance's method when called, to identify the instance that called it. "self" is used to access other attributes or methods of the object from inside the method. (methods are basically just functions that belong to a class)

"self" does not need to be used when calling a method when you already have an available instance.

Accessing the "some_attribute" attribute from inside a method:

class MyClass(object):
    some_attribute = "hello"

    def some_method(self, some_string):
        print self.some_attribute + " " + some_string

Accessing the "some_attribute" attribute from an existing instance:

>>> # create the instance
>>> inst = MyClass()
>>>
>>> # accessing the attribute
>>> inst.some_attribute
"hello"
>>> 
>>> # calling the instance's method
>>> inst.some_method("world") # In addition to "world", inst is *automatically* passed here as the first argument to "some_method".
hello world
>>> 

Here is a little code to demonstrate that self is the same as the instance:

>>> class MyClass(object):
>>>     def whoami(self, inst):
>>>         print self is inst
>>>
>>> local_instance = MyClass()

>>> local_instance.whoami(local_instance)
True

As mentioned by others, it's named "self" by convention, but it could be named anything.

monkut
  • 42,176
  • 24
  • 124
  • 155
  • 1
    +1 for making it clear that `local_instance` is passed on to the method, and that methods are just functions that are defined in classes. Thanks! – jrhorn424 Aug 10 '11 at 15:32
6

self refers to the current instance of Bank. When you create a new Bank, and call create_atm on it, self will be implicitly passed by python, and will refer to the bank you created.

recursive
  • 83,943
  • 34
  • 151
  • 241
6

I don't immediately grok what self is pointing to. This is definitely a symptom of not understanding classes, which I will work on at some point.

self is an argument passed in to the function. In Python, this first argument is implicitly the object that the method was invoked on. In other words:

class Bar(object):
    def someMethod(self):
        return self.field

bar = Bar()

bar.someMethod()
Bar.someMethod(bar)

These last two lines have equivalent behavior. (Unless bar refers to an object of a subclass of Bar -- then someMethod() might refer to a different function object.)

Note that you can name the "special" first argument anything you want -- self is just a convention for methods.

I understand that i points to an item in the list range(3) which, since it is in a function, isn't global. But what does self "point to"?

The name self does not exist in the context of that function. Attempting to use it would raise a NameError.


Example transcript:

>>> class Bar(object):
...     def someMethod(self):
...         return self.field
...
>>> bar = Bar()
>>> bar.field = "foo"
>>> bar.someMethod()
'foo'
>>> Bar.someMethod(bar)
'foo'
>>> def fn(i):
...     return self
...
>>> fn(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in fn
NameError: global name 'self' is not defined
cdhowie
  • 158,093
  • 24
  • 286
  • 300
3

One Rubyist's perspective (Ruby is my first programming language so I apologize for whatever oversimplified, potentially wrong abstractions I'm about to use)

as far as I can tell, the dot operator, for example:

os.path

is such that os gets passed into path() as its first variable "invisibly"

It is as if os.path is REALLY this:

path(os)

If there were a daisy chain I'd imagine that this:

os.path.filename

Would be sort of like this in reality*:

filename(path(os))

Here comes the offensive part So with the self variable all that's doing is allowing the CLASS METHOD (from a rubyist perspective python 'instance methods' appear to be class methods...) to act as an instance method by getting an instance passed into it as its first variable (via the "sneaky" dot method above) which is called self by convention. Were self not an instance

c = ClassName()
c.methodname

but the class itself:

ClassName.methodname

the class would get passed in rather than the instance.

OK, also important to remember is that the __init__ method is called "magic" by some. So don't worry about what gets passed into generate a new instance. To be honest its probably nil.

boulder_ruby
  • 38,457
  • 9
  • 79
  • 100
3

The reason "self" is there (by convention) is that when the Python runtime sees a call of the form Object.Method(Param1,Param2), it calls Method with parameters (Object,Param1,Param2). So if you call that first parameter "self", everyone will know what you are talking about.

The reason you have to do this is the subject of another question.

As far as a metaclass, it's something rarely used. You might want to look at: http://python-history.blogspot.com/2009/04/metaclasses-and-extension-classes-aka.html, the original author and current Benevolent Dictator For Life of Python explains what this is, and how it came to be. He also has a nice article on some possible uses, but most people never directly use it at all.

psr
  • 2,870
  • 18
  • 22
2

self refers to an instance of the class.

MRAB
  • 20,356
  • 6
  • 40
  • 33