[Tutor] a bug?

Michael P. Reilly arcege@speakeasy.net
Fri, 1 Feb 2002 16:25:48 -0500

On Fri, Feb 01, 2002 at 10:29:50AM -0600, Christopher Smith wrote:
> Would you consider the type returned by the 3rd line below to be an error?
>  If so, how do I report it?  I am running Python on a Mac.
> >>> type(id)
> <type 'builtin_function_or_method'>
> >>> type(len)
> <type 'builtin_function_or_method'>
> >>> type(type)
> <type 'type'>

Assuming you mean a bug in Python 2.2, then nope.

You could have a callable object, which is exactly what this is.  It is
equivalent to:

class type:
  def __call__(self, obj):
    """This would be interpreted internally in C as getting the "ob_type"
       object in the PyObject structure.
    return __c_builtin_retrieve_type_object(obj)
import __builtin__
__builtin__.type = type()

Then at this point, we could call type(1) and get the internal data type
of the object.

Since in Python 2.2, there is the unification of C types and classes,
you can now make a subclass of a type.  Notice:

>>> class A:
...   pass
>>> type(A)
<type 'class'>
>>> type(type(A))
<type 'type'>
>>> # we'll try to make <type 'type'> a called object, passing the class
>>> type(type(A)) (A)
<type 'class'>

So if we looked at type(type), we'd get <type 'type'>.

It sounds like circular logic.  But really "type" is the top of a tree
that includes "class", "integer", "string", "instance" and "object"
(which is also in the __builtin__ module).  In computer science, the
parent of the top is often the top itself (i.e.  one way to get to the
top of a tree is 'while node.parent is not node: node = node.parent').

>>> type is type(type)

This is exactly what's happening here.  Hope this helps.


PS: If it was a defect, then you probably would want to go to the
"SourceForge bug tracker" page for Python: