Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistence among built-in types and user defined

Tags:

python

types

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.

like image 579
Augusto Hack Avatar asked Nov 10 '13 16:11

Augusto Hack


People also ask

What is the difference between built-in type and user-defined type?

Data types define the type of value or data a variable holds in it. Built-in Data types are used to store simple data types such as integers, decimal point values, characters, etc. But Data also occurs in a wide variety of formats.

What are the differences between built-in function and user-defined function?

A defined function is one that you define in your own code. A built-in function is a function that already exists in the language without you having to define it.

What is pre defined and user-defined?

These functions are predefined in the compiler of C language. These function are created by user as per their own requirement. These functions are not created by user as their own. User-defined functions are not stored in library file. Library Functions are stored in special library file.


1 Answers

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?
like image 84
alko Avatar answered Oct 20 '22 00:10

alko