Everything is an object in python - object class and type class

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jun 3 04:32:42 EDT 2015


On Wednesday 03 June 2015 05:31, Jon Ribbens wrote:

> On 2015-06-02, Dr. Bigcock <dreamingforward at gmail.com> wrote:
>> On Tuesday, June 2, 2015 at 1:49:03 PM UTC-5, Jon Ribbens wrote:
>>> On 2015-06-02, Dr. Bigcock <dreamingforward at gmail.com> wrote:
>>> > It doesn't really do anything.  No one uses integers as objects.
>>> > (Any dissenters?)
>>> 
>>> Yes. *Everyone* uses integers as objects. Containers such as
>>> lists and dictionaries and tuples etc contain objects. If
>>> integers weren't objects then you wouldn't be able to put them
>>> in containers (and you'd end up with Java).
>>
>> Sorry.  I meant "object" in the sense of OOP:  something you might
>> extend or make a derived class with.
> 
> I'm not sure you get to define which properties of objects you want
> not to count.
> 
>> Your last claim, must not be true because integers were able to be
>> placed in objects before the type/class unification with v2.6, I
>> believe.
> 
> Unless I'm misremembering, before that they were still objects,
> just not quite the same kind of objects as pure-Python ones.

Correct. It was version 2.2, not 2.6, that unified built-in types with 
classes. Prior to that, Python had two distinct kinds of object, with 
separate hierarchies:


Types  (builtins, defined in C)
 +-- int
 +-- dict
 +-- str
 +-- list

Classes  (custom-made in Python using the class keyword)
 +-- Foo
 +-- Bar
      +-- FooBar
      +-  FooBarBaz


You could only subclass classes, not types. But *both* were kinds of 
objects. They were just separate, with slightly different characteristics.

Starting with 2.2, the old-style classic classes still existed, for 
backwards compatibility, but Python introduced a single base-class for the 
built-in types, called it "object", and enabled subclassing from pure-Python 
code:


Unified types/classes ("new-style classes")
 +-- object
     +-- int
         +-- MyInteger
     +-- dict
     +-- str
     +-- list
         +-- MyList
     +-- Spam

Classic Classes ("old-style classes")
[unchanged from above]


Finally, in Python 3, the classic classes were removed, and Python now has a 
single unified type/class hierarchy, with "object" at the root.


But regardless of whether Python had a single type hierarchy or two separate 
hierarchies, all values in Python were still implemented as objects.


-- 
Steve




More information about the Python-list mailing list