Difference between type and class

Nikolaus Rath Nikolaus at rath.org
Thu Jul 31 11:00:51 EDT 2008


Maric Michaud <maric at aristote.info> writes:
> Le Thursday 31 July 2008 14:30:19 Nikolaus Rath, vous avez écrit :
>> oj <ojeeves at gmail.com> writes:
>> > On Jul 31, 11:37 am, Nikolaus Rath <Nikol... at rath.org> wrote:
>> >> So why does Python distinguish between e.g. the type 'int' and the
>> >> class 'myclass'? Why can't I say that 'int' is a class and 'myclass'
>> >> is a type?
>> >
>> > I might be wrong here, but I think the point is that there is no
>> > distinction. A class (lets call it SomeClass for this example) is an
>> > object of type 'type', and an instance of a class is an object of type
>> > 'SomeClass'.
>>
>> But there seems to be a distinction:
>> >>> class int_class(object):
>>
>> ...   pass
>> ...
>>
>> >>> int_class
>>
>> <class '__main__.int_class'>
>>
>> >>> int
>>
>> <type 'int'>
>>
>> why doesn't this print
>>
>> >>> class int_class(object):
>>
>> ...   pass
>> ...
>>
>> >>> int_class
>>
>> <type '__main__.int_class'>
>>
>> >>> int
>>
>> <type 'int'>
>>
>> or
>>
>> >>> class int_class(object):
>>
>> ...   pass
>> ...
>>
>> >>> int_class
>>
>> <class '__main__.int_class'>
>>
>> >>> int
>>
>> <class 'int'>
>>
>> If there is no distinction, how does the Python interpreter know when
>> to print 'class' and when to print 'type'?
>>
>
> There are some confusion about the terms here.
>
> Classes are instances of type 'type',

Could you please clarify what you mean with 'instance of type X'? I
guess you mean that 'y is an instance of type X' iif y is constructed
by instantiating X. Is that correct?

> What the <type int> means is that int is not a user type but a
> builtin type, instances of int are not types (or classes) but common
> objects, so its nature is the same as any classes.
>
> The way it prints doesn't matter, it's just the __repr__ of any instance, and 
> the default behavior for instances of type is to return '<class XX>', but it 
> can be easily customized.

But 'int' is an instance of 'type' (the metaclass):

>>> int.__class__
<type 'type'>

so it should also return '<class int>' if that's the default behavior
of the 'type' metaclass.

I think that to get '<type int>' one would have to define a new
metaclass like this:

def type_meta(type):
    def __repr__(self)
         return "<type %s>" % self.__name__

and then one should have int.__class__ == type_meta. But obviously
that's not the case. Why?


Moreover:

>>> class myint(int):
...    pass
... 
>>> myint.__class__ == int.__class__
True
>>> int
<type 'int'>
>>> myint
<class '__main__.myint'>

despite int and myint having the same metaclass. So if the
representation is really defined in the 'type' metaclass, then
type.__repr__ has to make some kind of distinction between int and
myint, so they cannot be on absolute equal footing.



Best,

   -Nikolaus

-- 
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
                                                         -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C



More information about the Python-list mailing list