2

Possible Duplicate:
Python ‘self’ explained

I am a newbie for programming and python. I understand using self in class methods and declaring class variables, however, my question is: why isn't self used to declare variables outside of class methods?" I am unsure of the terminology so I apologize if this question has already been answered in another post.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 4
    Can you give an example of what you mean? "Class variable" and "class method" may not mean what you think they do in Python. – BrenBarn Nov 20 '12 at 19:56
  • Do you have a background in JavaScript? (Just a hunch) in that case you have to think different about objects... – Niclas Nilsson Nov 20 '12 at 20:04
  • `self` is usually the name given to the first argument of a class method when it is called and refers an _instance_ of the class not the class itself. Classes are objects, too, but their class is called a metaclass -- something you likely don't need to be concerned about yet. To refer to a class attribute you can just use the class's name, i.e. `My_class.attrib`. – martineau Nov 20 '12 at 20:14

3 Answers3

2

self is not used to declare variables anywhere. self is the usual name for the first parameter of a method, as the method receives the instance to which it is bound as the first parameter to every call.

It is not used "elsewhere" because it only applies in contexts where there is an instance for a callable to be bound to.

You are probably thinking of something like:

class Foo(object):
    y = 1
    x = self.y * 2

This is not possible because there is no self - in fact, at the time that the body of a class statement is evaluated, there is no instance or class object yet in existence. It is also not necessary, because one can simply refer directly to other variables in the scope of the body without using self.

Note that assignments to members of self, and assignments to variables in class scope do two different things:

class Foo(object):
    y = 1 # class variable
    def __init__(self):
        self.x = self.y * 2 # instance variable, defined by reference to class variable

first = Foo()
Foo.y = 2
first.y = 3
second = Foo()
print first.x # = 2
print second.x # = 4
Marcin
  • 48,559
  • 18
  • 128
  • 201
  • 1
    I guess I am confusing two concepts. In the __init__ method, i use self.Foo = 3 but when not in a method i would just use foo = 3. That is what confuses me – Aaron Booher Nov 20 '12 at 20:50
  • @AaronBooher And the reason why is as explained in this answer: you have to use `self` in a method to refer to the instance, but outside the method there is no instance to refer to. – Marcin Nov 20 '12 at 21:01
0

self is a convention - not a reserved Python word. The first argument in a regular method declaration represents the object. Nevertheless, it is a conventional wisdom not to use self outside classes - for the sake of code readability and consistency.

In general, you may use any word(s) as a variable identifier, but variable names are implicitly part of the code documentation, so it is a good practice to use names that are as much self-explanatory as you can squeeze into a 5-12 symbols (those are my preferences) - they may be longer, but the names that are too long clutter your code.

self outside the class is meaningless - it will not help code reader to understand the code. So you may use it if you like - but that will not be a good idea.

volcano
  • 3,578
  • 21
  • 28
0

This link (http://www.python.org/doc/essays/ppt/acm-ws/sld051.htm) may give you some clarification on class vs. instance variables which I what I believe you're asking about. Take a look at that page and the corresponding next slide.

And if that doesn't clear up what you're asking, as far as class methods I'm not sure if I know what you're referring to. I'd suggest reading about "classmethod"'s (http://docs.python.org/2/library/functions.html#classmethod) and "staticmethod"'s (http://docs.python.org/2/library/functions.html#staticmethod)

hkothari
  • 254
  • 3
  • 14
  • so in other words, a class variable is the same for each instance and a instance variable changes based on when the instance is created – Aaron Booher Nov 20 '12 at 20:58