type(type) is an object, not an instance 
Author Message
 type(type) is an object, not an instance

I don't know if this can be called a bug, but type(type) is the type
object, not an instance of it, like type of other types:
Quote:
>>> type(type) is type
1
>>> type(int) is int

0

type(type) is an object, therefore its methods are unbound, so this will
raise an error:

Quote:
>>> type(type).mro()

Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: descriptor 'mro' of 'type' object needs an argument

But type.mro works, if you specify the self argument manually:

Quote:
>>> type(type).mro(type)
>>> type.mro(type)

[<type 'type'>, <type 'object'>]

sipsick



Sun, 04 Sep 2005 10:09:22 GMT  
 type(type) is an object, not an instance

Quote:

> I don't know if this can be called a bug, but type(type) is the type
> object, not an instance of it

Actually, it's both! It's an instance of itself:

Python 2.2 (#1, Jul 11 2002, 14:19:37)
[GCC 3.0.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> isinstance(type, type)
1

--
Greg Ewing, Computer Science Dept,
University of Canterbury,      
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg



Sun, 04 Sep 2005 11:34:38 GMT  
 type(type) is an object, not an instance

Quote:

> I don't know if this can be called a bug, but type(type) is the type
> object, not an instance of it, like type of other types:
> >>> type(type) is type
>  1
> >>> type(int) is int
> 0

I was writing an "Introduction to metaclasses" (I don't know if I will
ever finish it, since I am always discovering new and new things about
them, and also because I am lazy ;).
Here is the beginning, that answer your question:

------------------------------------------------------------------------

In the python object model (inspired from the Smalltalk object model)
classes themselves are objects.
Now, since objects are instances of classes, that means that classes
themselves can be seen as instances of special classes called metaclasses.
That seems easy, but actually it gets hairy soon, since by following this
idea, one can say that metaclasses themselves are classes and therefore
objects:  this  means than metaclasses can be seen as instances of
special classes called meta-metaclasses. On the other hand, for the same
argument, meta-meta-classes can be seen as instances of meta-meta-metaclasses,
etc. Now, it should be obvious why metaclasses have gained such a reputation
of brain-exploders ;). However, fortunately, the situation is not so bad in
practice: the infinite recursion of metaclasses is avoided because there is a
metaclass that is the "mother of all metaclasses": the built-in metaclass
type. 'type' has the property of being its own metaclass, therefore the
recursion stops.
Consider for instance the following example:

  >>> class C(object): pass # a generic class
  >>> type(C) #gives the metaclass of C
  <type 'type'>
  >>> type(type(C)) #gives the metaclass of type
  <type 'type'>

The recursion stops, since the metaclass of 'type' is 'type'.
One cool consequence of classes being instances of 'type',
is that since *type* is a subclass of object,

   >>> issubclass(type,object)
   True

any Python class is not only a subclass of ``object``, but also
an instance of 'object':

   >>> isinstance(C,type)
   True
   >>> isinstance(C,object)
   True
   >>> issubclass(C,object)
   True

Notice that 'type' is an instance of itself (!) and therefore of 'object':

  >>> isinstance(type,type) # 'type' is an instance of 'type'
  True
  >>> isinstance(type,object) # therefore 'type' is an instance of 'object'
  True

------------------------------------------------------------------------

For a more formal analysis you can look at the book "Putting metaclasses
to work" by Forman and Danforth.

                                            Michele



Sun, 04 Sep 2005 21:32:57 GMT  
 type(type) is an object, not an instance
   ...

Quote:
> For a more formal analysis you can look at the book "Putting metaclasses
> to work" by Forman and Danforth.

...which is unfortunately out of print according to the site of its
publisher (Addison-Wesley).  Sigh.  Any advice for good suppliers of
exoteric technical out-of-print books...?

Alex



Mon, 05 Sep 2005 23:20:24 GMT  
 type(type) is an object, not an instance

Quote:


>    ...
> > For a more formal analysis you can look at the book "Putting metaclasses
> > to work" by Forman and Danforth.

> ...which is unfortunately out of print according to the site of its
> publisher (Addison-Wesley).  Sigh.  Any advice for good suppliers of
> exoteric technical out-of-print books...?

> Alex

I bought it from Amazon few months ago. I just checked and it is still
available:

http://www.amazon.com/exec/obidos/tg/detail/-/0201433052/qid=10481862...



Tue, 06 Sep 2005 02:54:09 GMT  
 type(type) is an object, not an instance

Quote:




>>    ...
>> > For a more formal analysis you can look at the book "Putting
>> > metaclasses to work" by Forman and Danforth.

>> ...which is unfortunately out of print according to the site of its
>> publisher (Addison-Wesley).  Sigh.  Any advice for good suppliers of
>> exoteric technical out-of-print books...?

>> Alex

> I bought it from Amazon few months ago. I just checked and it is still
> available:

Thanks for the tip!  Unfortunately, following the links to the sellers
(not Amazon itself, but one of the shops that sells used books through
them) ends up with a "We're sorry. This item can't be shipped to the
address you've selected" when I try to have it shipped to Italy.
Fortunately, I do have friends in the US willing to receive stuff for
me and mail it here.  However, once again I find myself wishing for
FAR more globalization than actually seems to have occurred so far...;-)

Alex



Tue, 06 Sep 2005 04:54:18 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. DupByteArrayInternalRep does not set object type of new object

2. type object of an instance without instantiation?

3. object must not be deeper than the access type

4. Types (was: Attribute vs Instance Variable)

5. 'Access on current instance of abstract type

6. Explicit interface of a type-bound procedure bounded to an instance (F2003)

7. Exception argument is of Type 'instance'?

8. class instance type comparisons, like isa()

9. changing slots in an overloaded initialize-instance, and type checking

10. Deriving from builtin types with limited instances and Extending FAQ 6.6

11. Deriving from builtin types with limited instances and Extending FAQ 6.6

12. Dictionary type access to list of instances

 

 
Powered by phpBB® Forum Software