7

While reading about the unification of types I stumbled on the fact that built-in types have method_descriptors and builtin_function_or_methods instead of methods and functions, why?

>>> list.append
<method 'append' of 'list' objects>
>>> type(list.append)
<class 'method_descriptor'>
>>> [].append
<built-in method append of list object at 0x7f0c4214aef0>
>>> type([].append)
<class 'builtin_function_or_method'>
>>> class A(list):
...   def append(self): pass
... 
>>> A.append
<function A.append at 0x7f0c42168dd0>
>>> type(A.append)
<class 'function'>
>>> A().append
<bound method A.append of []>
>>> type(A().append)
<class 'method'>

There is no good reason for class A to subclass list, I just wanted to show that the types differ.

Augusto Hack
  • 1,804
  • 16
  • 31

1 Answers1

3

Difference lies in fact that built-ins are C-compiled code descriptors, whereas user-defined functions represent iterpreted code descriptors. See source for details.

Also, while built-ins and their methods are statically allocated data structures, memory for user-defined data structures is allocated dinamically. Even sizes differs: size of descriptors is equal among built-in functions as well as among similar user-defined, refer to C source (link above):

>>> sys.getsizeof(list.append)
72   # built-in
>>> sys.getsizeof(dir)
72   # built-in
>>> sys.getsizeof(A.__init__)
80   # class/instance method
>>> sys.getsizeof(lambda x: x)
120  # static function

So those things look different, resides in different places and behave different. There is no need to give them equal names.

I would like to add missed compiled analogue for classmethod, classmethod_descriptor,

>>> type(float.__dict__['fromhex'])
<type 'classmethod_descriptor'>

and some other interesting types:

>>> type(A.__init__)
<type 'wrapper_descriptor'>
>>> type(A.__dict__['__dict__'])
<type 'getset_descriptor'>

See:

  1. What is a wrapper_descriptor, and why is Foo.__init__ one in this case
  2. What is the __dict__.__dict__ attribute of a Python class?
Community
  • 1
  • 1
alko
  • 39,930
  • 9
  • 90
  • 97
  • Thank you for the answer, I understand that there is a implementation difference, but I want to know why these methods need to be of different types. Is there a compatibility reason, or it just ended up being like that and did not create any problems? – Augusto Hack Nov 11 '13 at 00:50
  • @hack.augusto I tried to add some more explanation, see if it helps – alko Nov 16 '13 at 21:57