#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    Why use self.var = var in a method?


    OK, so, I've been reading some codes and in almost all of them, I see something like this:
    Code:
    class Example:
                 def meth1(self, wrapped):
                        self.wrapped = wrapped
                        return self.wrapped + 1
    So, why do this instead of just this:
    Code:
    class Example:
                 def meth1(self, wrapped):
                        return wrapped + 1
    Also, as far as I know, variables created like this become available to instances as well, so, is that the only reason why I see codes like this?
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    You do this to make wrapped available to other methods within the class.
    Once you learn how to construct a class properly this makes more sense.
    You also have to think about class inheritance.
    Last edited by Dietrich; November 17th, 2013 at 05:20 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by Dietrich
    You do this to make wrapped available to other methods within the class.
    Thanks! That's all I needed to know.

IMN logo majestic logo threadwatch logo seochat tools logo