1

On executing this code, I expected 0 but instead got [10]. The question is how does self work and should it be used while declaring variables local to a function within the class. In mod_a and mod_b both x.self is suppose to be local. The list is the result of the return in Line 4 or self.variable is a global variable declaration?

I have read the python documentation which refers to "this" of c++ but I don't know c++, therefore decided to ask here. Incase its been answered before please point me to it, I don't think I am using the right keywords in my search.

 1  class ExampleClass(object):
 2    def mod_a(self):
 3      self.x=[10]
 4      return self.x
 5
 6    def mod_b(self):
 7      self.x=0
 8      self.mod_a()
 9      return self.x
10
11  if __name__=="__main__":
12    s=ExampleClass()
13    print s.mod_b()

Output 
 python test.py
[10]
3
  • 4
    self is not a part of Python. It is a convention. Commented Feb 12, 2015 at 22:18
  • Could you please elaborate. It doesn't help solve my confusion. Commented Feb 12, 2015 at 22:19
  • You can write whatever you want as an argument to point to the current object. Commented Feb 12, 2015 at 22:21

2 Answers 2

3

self is the instance of the class that is currently being manipulated. Note, there is nothing magical about the name self -- It is just a python variable like anything else. Convention (strongly) dictates that the variable be named self however. In other languages, it is sometimes called this, so if you really wanted to buck the trend, you could write a class like this:

class Foo(object):
    def __init__(this):
        this.x = 12

Maybe to illuminate this a bit more, it would be worthwhile to print some Ids...

class Foo(object):
    def print_my_id(self):
        print(id(self))

    def print_my_id_delegator(self):
        self.print_my_id()

f = Foo()
print(id(f))
f.print_my_id()
f.print_my_id_delegator()      

Now, if you look at the printout, you'll notice that all the numbers (id's) are the same. So, when you write:

f.print_my_id()

python executes:

type(f).print_my_id(f)

which is the same as:

Foo.print_my_id(f)

Looking at it this way, you can see quite clearly what self is in this function call (it's f!)

Of course, we could reproduce this experiment with a different instance of Foo ...

g = Foo()
print(id(g))
g.print_my_id()

And now we see that python executed

Foo.print_my_id(g)

when you wrote that last line.


To bring this back to home with your code, in your case here, mod_b sets the x attribute on the instance to 0, but then it calls self.mod_a. The self in mod_a is the same self that was in mod_b, so it re-sets the value to [10].

Sign up to request clarification or add additional context in comments.

3 Comments

So within the scope of this class I am declaring a global variable by appending self to it, did I understand that correctly? Should I be using self to declare variables local to a function?
@Sameer -- I've updated my post. Hopefully this makes it more clear?
@Sameer You got the first part right... sort of. It's like a global var in that any variable prepended with self. is global to that instance's namespace and automatically becomes an attribute of that object. You can then change its value in one method and get it again using self.. If you want to declare variables local to a function, you don't prepend them with anything; by the time the function ends, the variable no longer exists.
2

self is a convention1 used in python to mean "this object" or "the current object". It refers to the object that was used to call the function. While it may look like a local variable by virtue of being a parameter, it is not. It is a reference to the object itself. The variable self in mod_a is exactly the same as self in mod_b -- they both refer to the same object. Because of that, self.x in one method refers to the very same self.x in another method.

self is the thing that allows all of the methods within an object to refer to a common set of data. Not global per se, just visible only within the methods of the object.

1 Note: self is just a convention. You can use any word you want. For example, the following code is completely acceptable according the rules of the language:

class ExampleClass(object):
  def mod_a(this_object):
    this_object.x=[10]
    return this_object.x

  def mod_b(this_object):
    this_object.x=0
    this_object.mod_a()
    return this_object.x

However, pretty much the whole python world uses self, so choosing some other word will make your code harder to understand.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.