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

Steven D'Aprano steve at pearwood.info
Sun May 31 14:00:49 EDT 2015


On Mon, 1 Jun 2015 12:34 am, Eddilbert Macharia wrote:

> Hello All ,
> 
> I'm wrecking my head trying to understand. where the class object comes
> into play .

This is just the standard object-oriented concept of class and instances.
Have you programmed in any other OO language? 

Lassie is an individual example of a dog. Lassie is an instance, dog is the
class.

42 is an individual example of an int (integer). 42 is an instance, int is
the class (also known as type).

In some languages, like Java, and Javascript, there are two "kinds" of
values: objects, and primitives. Primitives are the sorts of values you use
in low-level languages like C, Fortran or Pascal, or in assembly language:
typically things like numbers, arrays, strings, booleans (True and False).
These are sometimes called "unboxed" values, "boxed" values being another
name for objects.

Python is not like those languages -- all values in Python are objects, in
the sense of object-oriented programming.



> Is it only meant to act as base class and does it mean there is an actual
> class called object in python which all the objects created using the
> class type inherit ?

Yes, there is an actual class called "object" in Python:

py> object
<class 'object'>

In Python 3, all values are objects in BOTH the sense of OO programming, and
the sense that the inherit from the object class.

In Python 2, for historical reasons, *some* classes do not inherit from
object, but form a separate hierarchy called "classic" or "old-style"
classes. It's best to ignore them, if you can, since they add complexity
for little value. I will say no more about these classic classes, although
if you are really curious, ask and someone (possibly me) will answer.


> i'm assuming the metaclass if simplified would look something like this :
> 
> type('dict', (object,),{})
> 
> And when we use the class type as a metaclass are we using the instance
> version of the class type or are we actually using the type class itself ?

I don't understand what you are trying to ask.


Every instance belongs to a class: 42 is an instance of int, "spam and eggs"
is an instance of str. In Python, classes themselves are also values, and
hence are instances of some other class. This class-of-the-class is called
the metaclass.

int is a class, int is an instance of the metaclass called "type";
str is a class, str is an instance of the metaclass called "type";

etc. Normally, all the classes you come across will belong to the same
metaclass, type.

Since type itself is a class, what's its metaclass? (The "metametaclass",
you might say, although we never do :-) Well, type is special. The chain of
instance <- class <- metaclass <- metametaclass <- ... cannot go on
forever, and so the Python interpreter treats type as special, so that the
chain of meta-meta-meta-meta...classes has a beginning:

type -> int -> 42

The class of type is itself!

py> type(type) is type
True

So, when you create your own class:

class Spam(object):
    def method(self):
        return "spam spam spam"


Python treats this as syntactic sugar for a call to type:

Spam = type("Spam", (object,), {'method': function-object})


That's right, type() does two things! With a single argument, it returns the
type (class) of the value passed in:

py> type(42)
<class 'int'>


With three arguments, it creates a new class.

So if you use a metaclass, Python simply replaces the call to type(...) with
a call to the metaclass.

Typically, you won't use metaclasses *at all*, but for those rare times when
you do, the usual pattern is:

- create a subclass of type that does something different;
- specify that as the metaclass.


class Meta(type):
    ...


# Python 3 syntax
class MyClass(object, metaclass=Meta):
    ...

# Python 2 syntax
class MyClass(object):
    __metaclass__ = Meta
    ...


Then, instead of the MyClass class definition being syntactic sugar for:


MyClass = type("MyClass", (object,), {...})  # the default

it becomes:

MyClass = Meta("MyClass", (object,), {...})

So now MyClass is an instance of Meta, which is a subclass of type.


That's (almost!) all there is to it. I've actually simplified a few of the
hairier details and ignored some of the complexity, but as a broad
overview, this is about everything you need to know about metaclasses.



> Also when we say everything is an object in python, are we referring to
> the fact that everything is an instance of the class type or does it have
> to with the object class inherited ?

Yes to both of those!



> As can be attested by using type() function as below :
[...] 
> From my understanding this means all of this are instances of the class
> type. which means the class type was used to create this instances.

Correct.


> Now if i look at the __bases__ of all this objects i get :
> 
>>>> type.__base__
> <class 'object'>
>>>> type.__bases__
> (<class 'object'>,)
>>>> dict.__bases__
> (<class 'object'>,)
>>>> list.__bases__
> (<class 'object'>,)
>>>> int.__bases__
> (<class 'object'>,)
>>>> object.__bases__
> ()
> 
> This tells me that all of this objects inherit from the class object which
> has nothing to do with them being instances.

Also correct.




-- 
Steven




More information about the Python-list mailing list