0

Possible Duplicate:
Why do you need explicitly have the “self” argument into a Python method?

Why do I have to add this self parameter

    def function(self):
Community
  • 1
  • 1
  • http://stackoverflow.com/questions/68282/why-do-you-need-explicitly-have-the-self-argument-into-a-python-method - possible duplicate. -1 – verisimilitude Aug 11 '12 at 14:26

2 Answers2

1

Because when you define a method inside a class, it automatically creates a descriptor that passes the object instance as the first parameter. If you want to avoid this, use the @staticmethod decorator, or just define your functions outside the class.

As for why the language is designed this way, it wouldn't make sense to do it otherwise in a language without explicit variable creation. If you do a=2, how do you know whether it should be a local or an instance variable? Also, passing it explicitly is just a more elegant design in general.

Antimony
  • 37,781
  • 10
  • 100
  • 107
0

Explicit is better than implicit.

edhedges
  • 2,722
  • 2
  • 28
  • 61