-2

Possible Duplicate:
python 'self' explained

For this example:

class HelloWorld:

    def index(self):
        return "Hello world!"

The self is always passed to the function. What is the self, and why is it required to be passed to functions/methods all the time? Thanks.

Community
  • 1
  • 1
DNB5brims
  • 27,282
  • 47
  • 125
  • 188

2 Answers2

5

Please check this What is the purpose of self?

It provides a very good discussion point.

Community
  • 1
  • 1
ramdaz
  • 1,687
  • 1
  • 19
  • 42
1

Just as a side note: self is actually just a randomly chosen word, that everyone uses, but you could also use this, foo, or myself or anything else you want, it's just the first parameter of every non static method for a class. This means that the word self is not a language construct but just a name:

class priyank_patel:
    def __init__(priyank):
       priyank.bla = 2
Priyank Patel
  • 3,452
  • 18
  • 20
  • 3
    -0. It would be such a bad idea to break the convention; imagine having to maintain code that someone else wrote.. Also, it's generally best not to confuse someone new to the whole concept by muddying the waters. :-) – Martijn Pieters Jun 04 '12 at 09:18
  • 1
    @MartijnPieters i know its break convention. i just explaining that first parameter can be anything valid variable name. – Priyank Patel Jun 04 '12 at 09:19