2 Replies - 2031 Views - Last Post: 30 April 2013 - 06:35 AM Rate Topic: -----

#1 Static Hazard   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 25-November 09

Trouble understanding "self"

Posted 29 April 2013 - 09:25 PM

Hi, I'm new to Python. I've been using the word "self" a lot because that's what I've seen in other programs, but I don't really know why. So when it comes to this error I'm having, I don't really know how to solve it. Here is simple program, where items are added to a repository.

class Item(object):

    def __init__(self):
        self.repository = []
    def add(self, item = None):
        if(item == None):
            raise ValueError("Item.add: Missing item value")
            self.theItem = item
        return len(self.repository)

test = Item()
print test.add(item = "book")

All I'm doing is adding a value to a repository (and checking to see if the add() call isn't empty). Why won't it let me print the list?

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble understanding "self"

#2 Mekire   User is offline

  • D.I.C Head

Reputation: 118
  • View blog
  • Posts: 216
  • Joined: 11-January 13

Re: Trouble understanding "self"

Posted 29 April 2013 - 10:44 PM

self is used (by convention not mandate) as the way that classes refer to themselves within their own body.
self is a reference to the instance of the class.

When you create an instance called test, and you want to access any attribute or method of the instance you type:

Then when the class runs these lines it replaces self with test.

It is also important to note that when you type test.method(arg) the first argument passed to method is not arg; it is test. You could also write Item.method(test,arg) if you were so inclined (of course in general no one does this but there are some cases where you need to). This is the reason that when we write class methods, the first argument is always self.

Anyway, I'm not particularly good at explaining but basically it comes down to this; you need to change that last line to:

Was This Post Helpful? 2
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon

Reputation: 7181
  • View blog
  • Posts: 14,969
  • Joined: 16-October 07

Re: Trouble understanding "self"

Posted 30 April 2013 - 06:35 AM

The original Python class system is almost a trick, really. It wires up just enough, but little more.

The self is the instance of the calling object. For an instance of a class, the "." notation just passes it transparently. Most other OO languages do the same thing, but the declaration of the instance passed is usually not give. Python chose to explicitly require the argument declaration, for a various reasons.

You can investigate this trick.
>>> def showThing(thing):
...     print("thing: " + str(thing.x))
>>> class Foo:
...     def __init__(self, x):
...             self.x = x
...     def show(self):
...             showThing(self)
>>> class Bar:
...     def __init__(self): self.x = 42
>>> a = Foo(144)
>>> b = Bar()
>>> a.show()
thing: 144
>>> Foo.show(a)
thing: 144
>>> showThing(a)
thing: 144
>>> showThing(b)
thing: 42
>>> Foo.show(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method show() must be called with Foo instance as first argument (got Bar instance instead)

Notice how a simple function showThing doesn't really care what it gets as long as it has an x? Most of python is like that, you pass values on assumed contract.

I'll admit, I was little offended that the class did a type check. Has it always done that? I'm not sure.

This post has been edited by jon.kiparsky: 30 April 2013 - 07:34 AM
Reason for edit:: undo smiley damage

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1