2

Given the example:

>>> class Cool: 
...    def mthd(self, val): 
...        self.val = val 
...

>>> def x(): pass

>>> Cool.mthd(x, 360)      # Works in 3.X only, not in 2.X 

2.X raises **TypeError: unbound method...** error, but in 3.x it works just fine.

Why does Python 2.X restricts self argument to be only an instance type, whereas Python 3.X doesn't restrict it to any data type? And why was this changed in 3.X?

GIZ
  • 4,409
  • 1
  • 24
  • 43
  • 2
    Possible duplicate of [Static methods in Python?](http://stackoverflow.com/questions/735975/static-methods-in-python) –  Feb 21 '16 at 09:10

1 Answers1

1

I don't know why it was changed (I guess to remove a concept that didn't add a lot of value), but the change is documented in the 'What’s New In Python 3.0' documentation:

The concept of “unbound methods” has been removed from the language. When referencing a method as a class attribute, you now get a plain function object.

With this change,

my_cool.mthd(360)

just becomes a (very recommended!) short hand notation for

type(my_cool).mthd(my_cool, 360)
das-g
  • 9,718
  • 4
  • 38
  • 80
  • 2
    I don't _know_ the reason either, but the change allows one to use classes as simple namespaces which are supposedly "one honking great idea" according to [PEP 10 - The Zen of Python](https://www.python.org/dev/peps/pep-0020/). – martineau Feb 21 '16 at 12:52
  • 1
    Also, "Simple is better than complex." And not having the concept of “unbound methods” is clearly simpler than having it. Also, less complicated. Could also be a case of "[P]racticality beats purity." – das-g Feb 21 '16 at 12:58
  • Perhaps the idea is to make dual usages for classes' methods, one as a methods mode and the other as simple functions. – GIZ Feb 21 '16 at 16:04