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

Eddilbert Macharia edd.cowan at gmail.com
Wed Jun 3 00:16:48 EDT 2015


On Tuesday, June 2, 2015 at 2:27:31 PM UTC+3, Steven D'Aprano wrote:
> On Tue, 2 Jun 2015 08:36 pm, Eddilbert Macharia wrote:
> 
> > you guys are just confusing me, you are going in loops, and still i have
> > understood ,what makes everything in python an object. hey is where i'm at
> > : *** type in python refers to data types e.g. int, str, boolean e.t.c.
> > right ?
> 
> Yes. Also classes you create with the "class" keyword:
> 
> class K(object):
>     ...
> 
> K is now a "type", just like int, str, list, object, etc.
> 
> 
> > *** The interpreter creates two classes type and object when setting up a
> > python environment. right ?
> 
> Many more than just two: it also creates list, str, dict, etc. But *first*
> it has to create type and object. So you are correct.
> 
> 
> > *** The creator (metaclass) of all data types (i.e. int,str) in python is
> > the class type. right ?
> 
> Correct.
> 
> [Aside: I'm only talking about Python 3 here. In Python 2 there is also a
> second hierarchy of classes, called "classic classes" or "old-style
> classes", which are *not* subclasses of type. But let's just ignore them,
> because they are gone in the most recent versions of Python.]
> 
> 
> >>>> isinstance(int,type)
> > True
> > 
> > *** The instance of class type is a data type an instance of class type.
> > right ?
> >>>> type(type)
> > <class 'type'>
> 
> type has many instances, not just one.
> 
> Instances of int are individual ints, like 0, 1, 2, 3, ...
> 
> Instances of type are individual types, like int, dict, str, list, ...
> 
> But one of those many instances of type is, yes, type itself! So *one* of
> the instances of type is type, which is also an instance of itself:
> 
> >>>> isinstance(type,type)
> > True
> 
> Correct. This makes type special. Most types are *not* instances of
> themselves:
> 
> py> isinstance(int, int)
> False
> 
> 
> > *** Class type gets some of its behavior from class object through
> > inheritance.right ?
> > 
> >>>> issubclass(type,object)
> > True
> 
> Correct.
> 
> 
> > *** instance of class object is type, in the sense it created using class
> > type which inherits from class object.right ?
> > 
> >>>> isinstance(object,type)
> > True
> >>>> isinstance(object,object)
> > True
> > 
> > ****so is it right to say, everything in python is an object because they
> > are instance of the class type which inherits from class object ?
> 
> No! That's not what we mean when we say "everything is an object".
> 
> Eddilbert, have you programmed in any other languages? It would help you
> understand if you have.

Sadly yes i have worked with java, and that is what is causing me so much grief.In java objects are instance of a class.pretty simple.

> 
> "Object" has a general meaning in computer science and programming, it is a
> compound data structure that is explicitly linked to a type which provides
> functionality that operates on that data structure.
> 
> In the programming language C, *no* values are objects. C has types (int16,
> uint32, bool, and many more) but no objects.
> 
> In the programming language Java, *some* values are objects, and some values
> are not objects.
> 
> In the programming language Python, *all* values are objects, in the general
> sense. That is what we mean by "Everything is an object".
> 
> Let's go back in time about 15 years or so. We're now using Python 1.5. In
> Python 1.5, there is no built-in object, and type is just a function, not a
> class:
> 
> >>> import sys
> >>> print sys.version
> 1.5.2 (#1, Aug 27 2012, 09:09:18)  [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)]
> >>> object
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> NameError: object
> >>> type
> <built-in function type>
> 
> 
> In Python 1.5, classes you create do not inherit from object, because object
> does not exist! BUT even in Python 1.5, it is true that everything is an
> object.
> 
> Remember, "object" can refer to two things:
> 
> - *specifically* the class called "object";
> 
> - the *general concept* of objects, from object oriented programming.
> 
> 
> In Python 1.5:
> 
> - everything is an object [the general concept]
> 
> - nothing is an instance of the class called "object"
> 
> 
> In Python 2:
> 
> - everything is an object [the general concept]
> 
> - some things, but not all things, are instances of the class
>   called "object"
> 
> 
> In Python 3:
> 
> - everything is an object [the general concept]
> 
> - everything is an instance of the class called "object"
> 
> 
> 
> 

I think then in python object and instance of a class are entirely different things.

in OOP and python - object is a representation of a real thing, or a concept .e.g a person,number and the concept of classes- which is the concept of create/representing other objects using a programming language to the machine.

class - This is what is used to create/represent objects in the machine using a programming language

class instance - This is the output of the classes this is a representation of an object.


> -- 
> Steven




More information about the Python-list mailing list