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

classic Classic list List threaded Threaded
92 messages Options
12345
Reply | Threaded
Open this post in threaded view
|

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

Eddilbert Macharia
Hello All ,

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

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 ?

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 ?

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 ?

As can be attested by using type() function as below :

>>> type(int)
<class 'type'>
>>> type(list)
<class 'type'>
>>> type(dict)
<class 'type'>
>>> type(type)
<class 'type'>
>>> type(object)
<class 'type'>

>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.

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.


Reply | Threaded
Open this post in threaded view
|

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

Marco Buttu-2
On 31/05/2015 16:34, Eddilbert Macharia wrote:

> 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 ?

 From Wikipedia: "In the class-based object-oriented programming
paradigm, `object` refers to a particular instance of a class"

So, because in Python everything is an instance of the class object
(isinstance(obj, object) is always True), everything is an object.
Only classes are instance of type. That means is worth to differentiate
between classes and non-classes, but it is a non-sense to differentiate
between classes and objects. If it could be useful:

http://marco-buttu.github.io/pycon_objmodel/

> As can be attested by using type() function as below :
>
>>>> >>>type(int)
> <class 'type'>
>>>> >>>type(list)
...
> >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.

Yes, that is true, because all these objects are classes

> Now if i look at the __bases__ of all this objects i get :
>>>> >>>type.__base__
> <class 'object'>
>>>> >>>type.__bases__
> (<class 'object'>,)
> ...

> This tells me that all of this objects inherit from the class object which has nothing to do with them being instances.

Take a look at all the steps that anticipate this diagram:

http://marco-buttu.github.io/pycon_objmodel/?full#Diagram

Every class inherits from object, and that implies every instance of a
class is an instance of object. And classes are instance of a class
(thier metaclass), so they are instance of objects too

--
Marco Buttu

INAF-Osservatorio Astronomico di Cagliari
Via della Scienza n. 5, 09047 Selargius (CA)
Phone: 070 711 80 217
Email: mbuttu at oa-cagliari.inaf.it



Reply | Threaded
Open this post in threaded view
|

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

Terry Reedy
In reply to this post by Eddilbert Macharia
On 5/31/2015 10:34 AM, Eddilbert Macharia wrote:

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

Leaving custom metaclasses aside.

1. All python data values are instances of some class.

2. Every class is a instance of the metaclass 'type'.

3. Every class is a subclass of the base class 'object'.

Some implications.

4. All objects, including classes, get their basic instance behaviors
from 'object', except as over-riden by other classes in the inheritance
chain.  These basic behaviors include default comparisons and
representations.  See 'dir(object)'.

5. Classes get their special class behaviors from 'type'.

--
Terry Jan Reedy



Reply | Threaded
Open this post in threaded view
|

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

Steven D'Aprano-8
In reply to this post by Eddilbert Macharia
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


Reply | Threaded
Open this post in threaded view
|

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

Eddilbert Macharia
In reply to this post by Eddilbert Macharia

So what im getting from above reponse, everything in python is an object because the are instance of the metaclass type and also because they are subclasses of the class object ?


On Sunday, May 31, 2015 at 5:34:20 PM UTC+3, Eddilbert Macharia wrote:

> Hello All ,
>
> I'm wrecking my head trying to understand. where the class object comes into play .
>
> 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 ?
>
> 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 ?
>
> 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 ?
>
> As can be attested by using type() function as below :
>
> >>> type(int)
> <class 'type'>
> >>> type(list)
> <class 'type'>
> >>> type(dict)
> <class 'type'>
> >>> type(type)
> <class 'type'>
> >>> type(object)
> <class 'type'>
>
> 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.
>
> 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.

Reply | Threaded
Open this post in threaded view
|

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

Eddilbert Macharia
In reply to this post by Eddilbert Macharia
Thank you for you responses guys.

So what I'm getting from above responses, everything in python is an object because the are instance of the metaclass type (from Steven response) and also because they are subclasses of the class object (from Marco and Terry response)?
I'm having headache trying to understand the cyclic relationship that exit between the metaclass type, the object class, and the class type.

I'm trying to understand how python makes everything an object.is it because everything is an instance of the metaclass type or is it because everything is a subclass of object class.

if its because of being subclass of object class, does that mean if the class object was named class pyobj. would that make everything python pyobj ?

i know objects created by metaclass are types/classes, this types are then used to create other object.

>From this :  
`>>> isinstance(type, object)
True`  
`>>> isinstance(object,type)
True`  
`>>> issubclass(object,type)
False`  
`>>> issubclass(type,object)
True`  

Is it safe to say that python creates the class object first using the type metaclass (I'm simplifying the metaclass for brevity).

`type('object',(),{})`  

which implies class object is a class of class type and it does not inherit any attributes other class.

Then it creates the class type

`type('type', (object,),{})`  

implying type class is class of class type and it inherits attributes from the object class.

Then creates the other classes by inheriting from the class object

`type('dict', (object,), {})`  
`type('Animal', (object), {})`

which similar to creating an Animal class as :
   
    `class Animal:
         pass`

Does this mean the metaclass used to create the class object is still the one used to create this Animal class or is the metaclass type used by default ?

Which type is being used, is it the metaclass type or the type class that was created after object was created ?

Where does the class type created with the base class object come into play ?

I have also tried to understand what really is going on between the object and the class from all he responses above and in this article http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html

But i'm still getting confused.What is the relation between this two class in terms of object creation.

will i ever get this or is it a chicken and egg situation ?




On Sunday, May 31, 2015 at 5:34:20 PM UTC+3, Eddilbert Macharia wrote:

> Hello All ,
>
> I'm wrecking my head trying to understand. where the class object comes into play .
>
> 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 ?
>
> 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 ?
>
> 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 ?
>
> As can be attested by using type() function as below :
>
> >>> type(int)
> <class 'type'>
> >>> type(list)
> <class 'type'>
> >>> type(dict)
> <class 'type'>
> >>> type(type)
> <class 'type'>
> >>> type(object)
> <class 'type'>
>
> 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.
>
> 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.


Reply | Threaded
Open this post in threaded view
|

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

Steven D'Aprano-8
In reply to this post by Eddilbert Macharia
On Mon, 1 Jun 2015 10:29 am, Eddilbert Macharia wrote:

>
> So what im getting from above reponse, everything in python is an object
> because the are instance of the metaclass type and also because they are
> subclasses of the class object ?

No.

Everything in Python is an object because Python has no "primitive"
or "unboxed" types, no machine values. This has nothing to do with
metaclasses. Even if Python had no metaclasses, it could still be true
that "everything is an object".

To summarise:

(1) In Python, "everything is an object" (that is, all values are objects)
is because Python does not include any primitive unboxed values. Anything
which can be used as a value (int, str, float, functions, modules, etc) are
implemented as objects.

(2) Python *also* has metaclasses. This does not follow from the above --
they are independent statements about the language. We could, if we want,
invent new languages:

- one where everything is an object, just like Python, but lacking
metaclasses;

- one which has metaclasses, just like Python, but not everything is an
object;

- and one which *neither* has metaclasses, *nor* everything is an object.

(Java is an example of the third one -- it has something kinda-sorta like
metaclasses, but it's just a tool for reflection, not the real thing.)


If that's enough, you can stop reading. But if you want more detail:


We can characterise various kinds of things in a programming language:

(1) Simple machine types, like bytes, integers, floats. These have no
internal structure, apart from individual bits. These are sometimes
called "machine primitives", and are the sorts of things you work with in
assembly language.

(2) Compound or structured machine types. These are made up of one or more
simple machine type, and include things like C structs and arrays.

(3) Objects, which are a more complex kind of compound type. More
importantly, objects involve a way of thinking about programming: some form
of inheritance, for example. Physically, an object is just a struct. But
it's a struct with a specific kind of meaning to the programming language,
which allows the language to provide inheritance, runtime types, and
various other goodies involved in Object Oriented Programming.

A language can choose to allow any or these, or all of these:

- assembly languages typically only support the most primitive, simple
machine types;

- languages like C and Pascal supports only simple and compound machine
types;

- some languages include both simple machine primitives and objects, such as
Java and Javascript;

- other languages don't offer any direct access to machine primitives, so
all values are objects, for example Python and Ruby.


To give a concrete example, consider a language with an "integer" type. In
C, an integer will be a machine primitive, a single, unstructured chunk of
memory with typically 32 bits, and that's all there is to it. In Python, an
integer will be an object, a large block of *structured* memory, typically
over 100 bits long, and different parts of that structure are used
internally to the interpreter.

A separate issue is whether or not classes are values. In some languages,
classes are *not* values: you cannot pass a class as argument to a
function, or assign it to a variable. In languages like these, classes are
not things (values), so "everything is an object" remains true even though
classes are not objects. Java is an example of that.

In Python, classes *are* variables. So we could have:

"In Python, everything is an object, or a class";

or

"In Python, everything is an object, including classes".


Which is true? The second is true: classes themselves are objects.

Finally, we have one last feature: does the language provide metaclasses?
Python does. Ruby, which is otherwise very similar to Python, does not:

In Python, everything is an object, and we have metaclasses.

In Ruby, everything is an object, but we don't have metaclasses.

http://ruby.dzone.com/news/ruby-doesnt-have-meta-classes



--
Steven


Reply | Threaded
Open this post in threaded view
|

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

Steven D'Aprano-8
In reply to this post by Eddilbert Macharia
On Mon, 1 Jun 2015 01:09 pm, Eddilbert Macharia wrote:

> Thank you for you responses guys.
>
> So what I'm getting from above responses, everything in python is an
> object because the are instance of the metaclass type (from Steven
> response) and also because they are subclasses of the class object (from
> Marco and Terry response)? I'm having headache trying to understand the
> cyclic relationship that exit between the metaclass type, the object
> class, and the class type.
>
> I'm trying to understand how python makes everything an object.is it
> because everything is an instance of the metaclass type or is it because
> everything is a subclass of object class.

Neither. You have the relationship confused.

Everything is an *instance* of the *class*, not the metaclass:

py> isinstance(42, type)  # type is the metaclass
False
py> isinstance(42, int)  # int is the class of 42
True


Most things are *not* subclasses of object:

py> issubclass(42, object)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 1 must be a class



> if its because of being subclass of object class, does that mean if the
> class object was named class pyobj. would that make everything python
> pyobj ?

No. "Object" is a standard term from computer science, Python could
rename "object" to "wibble", and we would still say "everything is an
object.

There are *two* (actually three) common meanings for the word "object" in
Python programming:

- the general meaning of object, as in Object Oriented Programming, shared
with hundreds of other languages;

- the specific, Python meaning, as in the built-in class called "object";

- and more rarely, as a synonym for "instance", as opposed to "class".


"Everything is an object" is understood in terms of Object Oriented
Programming. It was true way back in Python version 1.5, before the
built-in "object" class existed. It would still be true even if the
built-in "object" class was renamed to "wibble".

This:

py> isinstance(23, object)
True

is true because "object" is also the base class for everything in Python.
But that's a separate meaning.



[...]
> Is it safe to say that python creates the class object first using the
> type metaclass (I'm simplifying the metaclass for brevity).

No. type and object are special, and have to be bootstrapped into existence
by the interpreter. You cannot create type first, because type is an
object, so object needs to exist first. But you cannot create object first,
because object is a type, and so type has to exist.

You cannot solve this problem by creating one first, then the other. The
interpreter has to create them both.

[..]
> will i ever get this or is it a chicken and egg situation ?

Yes, exactly. You cannot break the cycle from Python code, it has to be done
deep inside the interpreter.




--
Steven


Reply | Threaded
Open this post in threaded view
|

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

Eddilbert Macharia
In reply to this post by Eddilbert Macharia
Thank you all who have taken there time to assist me.
Special thanks to Steven D'Aparo even though you didn't have two you have taken you time to go into details.

I think i kind of understand now.

Instead of python having data types like int, string, e.t.c it has two primitive types which are class type and class object which are created by python interpreter during its setup .using this two classes python is able to create some more data types

In some context type means the data type an object is e.g. an int,string e.t.c.

In the new class style, type and class sort of mean the same thing.

**type type is used to create new types when used as a metaclass
**type object is used to provide some default behavior and attributes to this types through inheritance

The interpreter makes sure of the following

** The type type inherits from data type object which makes the following statements are true

>>> issubclass(type,object)
True # type inherits object as its parent

>>> isinstance(type,object)
True # because of the inheritance

>>> type.__bases__
(<class 'object'>,) # because of the inheritance

>>> isinstance(type,type)
True # because metaclass type was used to create this data type type

** The type object is created using the type type i.e. type object is an instance of type type. The type object does not subclass i.e. inherit from any other types.

>>> isinstance(object,type)
True # because metaclass type was used to create this object

>>> issubclass(object,type)
False # because object does not inherit from any other class

>>> object.__bases__
() # because object does not inherit from any other class



On Sunday, May 31, 2015 at 5:34:20 PM UTC+3, Eddilbert Macharia wrote:

> Hello All ,
>
> I'm wrecking my head trying to understand. where the class object comes into play .
>
> 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 ?
>
> 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 ?
>
> 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 ?
>
> As can be attested by using type() function as below :
>
> >>> type(int)
> <class 'type'>
> >>> type(list)
> <class 'type'>
> >>> type(dict)
> <class 'type'>
> >>> type(type)
> <class 'type'>
> >>> type(object)
> <class 'type'>
>
> 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.
>
> 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.


Reply | Threaded
Open this post in threaded view
|

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

zipher
In reply to this post by Steven D'Aprano-8
On Sunday, May 31, 2015 at 1:01:03 PM UTC-5, Steven D'Aprano wrote:
> 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?

The issue here is:  which one is the object?  The class or the instance?

;^),

-m

Reply | Threaded
Open this post in threaded view
|

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

VernM
In reply to this post by Eddilbert Macharia
On Sunday, May 31, 2015 at 7:34:20 AM UTC-7, Eddilbert Macharia wrote:

> Hello All ,
>
> I'm wrecking my head trying to understand. where the class object comes into play .
>
> 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 ?
>
> 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 ?
>
> 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 ?
>
> As can be attested by using type() function as below :
>
> >>> type(int)
> <class 'type'>
> >>> type(list)
> <class 'type'>
> >>> type(dict)
> <class 'type'>
> >>> type(type)
> <class 'type'>
> >>> type(object)
> <class 'type'>
>
> 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.
>
> 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.

It may be helpful to try "dir(object)" to see what methods object provides to all those other classes.

Best regards, Vern

Reply | Threaded
Open this post in threaded view
|

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

BartC-3
In reply to this post by Eddilbert Macharia
On 01/06/2015 13:03, Eddilbert Macharia wrote:

> Thank you all who have taken there time to assist me.
> Special thanks to Steven D'Aparo even though you didn't have two you have taken you time to go into details.
>
> I think i kind of understand now.
>
> Instead of python having data types like int, string, e.t.c it has two primitive types which are class type and class object which are created by python interpreter during its setup .using this two classes python is able to create some more data types
>
> In some context type means the data type an object is e.g. an int,string e.t.c.
>
> In the new class style, type and class sort of mean the same thing.

I'm developing a new language along the lines of Python, perhaps a brief
description of how things are done there might help. Or just give a
different perspective.

Objects in this language are tagged: there's a code attached to each
indicating what kind of data is stored. These codes are integers, or
enumerations, for example:

  Int =    1
  String = 2
  List =   3
  Class =  4

And the following are examples of object instances with their tags:

a is (Int,    42)
b is (String, "ABCXYZ")
c is (List,   [10,20,30,40])
d is (Class,  2 or <String>)

The last one might be tricky to grasp: it's really just a number, but
one that represents a class or type (or tag). If printed, it could
display <String> rather than just 2. (And when used to do something with
the class, the 2 might be an index into a set of tables.)

d is not /the/ class itself, but just a reference to it (this is
pseudo-code not Python):

print (b)                 =>  ABCXYZ
print (typeof(b))         =>  String <2>

print (d)                 =>  String <2>
print (typeof(d))         =>  Class  <4>
print (typeof(typeof(d))) =>  Class  <4>

In my own language, the connection between class and type is hazy (I've
only just added classes so it needs to be hazy until I understand them
more).

'Type' includes everything, including all the built-in types such as the
tags above, but also user-defined classes. In fact classes are the
mechanism used to define new types. But with both designated by an
integer within the same band (eg. built-int types 1 to 20, user-defined
classes 21 and up), it is easier not to have a strong distinction ... at
the moment.

I haven't a root class yet that is the base of all the others. I don't
think it's necessary internally to make things work. But it might be a
useful concept in the language. Calling it 'object' however might give
rise to confusion as 'object' is informally used to refer to instances.
(I've never used OO but have picked up some of the jargon!)

(This is almost certainly not how Python does things. Although the
Python language doesn't go into details as to its implementation
provided the behaviour is correct.)

--
Bartc

Reply | Threaded
Open this post in threaded view
|

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

zipher
In reply to this post by Eddilbert Macharia
On Monday, June 1, 2015 at 7:03:49 AM UTC-5, Eddilbert Macharia wrote:
> I think i kind of understand now.
>
> Instead of python having data types like int, string, e.t.c it has two primitive types which are class type and class object which are created by python interpreter during its setup .using this two classes python is able to create some more data types

Eddibert, don't let this crowd (Terry, Stephen, et al.) confuse you.  They are as confused about it as you are.  

The truth is, that they've told a lie to themselves (everything is an object) to hide some details FROM THEMSELVES in their model.  They climb a ladder of ambiguity anytime there's a new problem in their model and call it a "type", or they pull out the "metaclass" term (because no one really knows that that is, so they feel *pretty* safe).

A type is not an object in the same way an instantiated type is an object -- anymore than a blueprint for a building is the building itself.

The reason this confusion appears is for the exact reason I said on an earlier thread:  Python does not define classes in it's grammar -- so it can't account for the difference between an [instantiated] object and it's definition/type.

Mark

Reply | Threaded
Open this post in threaded view
|

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

Chris Angelico
On Tue, Jun 2, 2015 at 10:24 AM, TheDoctor <dreamingforward at gmail.com> wrote:

> On Monday, June 1, 2015 at 7:03:49 AM UTC-5, Eddilbert Macharia wrote:
>> I think i kind of understand now.
>>
>> Instead of python having data types like int, string, e.t.c it has two primitive types which are class type and class object which are created by python interpreter during its setup .using this two classes python is able to create some more data types
>
> Eddibert, don't let this crowd (Terry, Stephen, et al.) confuse you.  They are as confused about it as you are.
>
> The truth is, that they've told a lie to themselves (everything is an object) to hide some details FROM THEMSELVES in their model.  They climb a ladder of ambiguity anytime there's a new problem in their model and call it a "type", or they pull out the "metaclass" term (because no one really knows that that is, so they feel *pretty* safe).
>
> A type is not an object in the same way an instantiated type is an object -- anymore than a blueprint for a building is the building itself.

And don't let dreamingforward confuse you, because he is flat-out
wrong. An integer is an object, because you can manipulate it as one.
A function is an object. A dictionary is an object. And a type is an
object too. There is no significant difference here.

ChrisA

Reply | Threaded
Open this post in threaded view
|

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

zipher
In reply to this post by zipher
On Monday, June 1, 2015 at 7:33:11 PM UTC-5, Chris Angelico wrote:
> On Tue, Jun 2, 2015 at 10:24 AM, TheDoctor <dreamingforward at gmail.com> wrote:
> > A type is not an object in the same way an instantiated type is an object -- anymore than a blueprint for a building is the building itself.
>
> And don't let dreamingforward confuse you, because he is flat-out
> wrong. An integer is an object, because you can manipulate it as one.

An integer in Python *acts* like an object, in some narrow sense can indeed be said to BE one.  But no one uses them like that in practice (purity was trying to beat practicality in the case of ints) -- they use them like ints, with the extra super-nice shortcuts of having them arbitrarily-sized.  And that was available before the type-class unification.

> A function is an object. A dictionary is an object.

Yep.

> And a type is an
> object too.

OOPs.

Mark

Reply | Threaded
Open this post in threaded view
|

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

zipher
In reply to this post by zipher
On Monday, June 1, 2015 at 7:33:11 PM UTC-5, Chris Angelico wrote:
> And a type is an
> object too. There is no significant difference here.

Let me make this clearer to you, Chris, because I don't want you to have to suck it too, like the rest of this community.

A type is not an object.  You see it as one, because you are MENTALLY lexing your own code on the screen.  But python does not define a type.  It defines certain primitives, like strings and integers.  But it doesn't define what an OBJECT is.  So you and everyone else are beating around the bush trying to define something that the LANGUAGE ITSELF doesn't define.  So, don't make a claim about it.  

In Python 2.7 type(type) is type (not object), but isinstance(type, object) is true -- so it is ambiguous, even contradictory, in the language.

Boom.  Suck it.

Mark

Reply | Threaded
Open this post in threaded view
|

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

Chris Angelico
On Tue, Jun 2, 2015 at 11:15 AM, TheDoctor <dreamingforward at gmail.com> wrote:
> Let me make this clearer to you, Chris, because I don't want you to have to suck it too, like the rest of this community.
>
> A type is not an object.  You see it as one, because you are MENTALLY lexing your own code on the screen.  But python does not define a type.  It defines certain primitives, like strings and integers.  But it doesn't define what an OBJECT is.  So you and everyone else are beating around the bush trying to define something that the LANGUAGE ITSELF doesn't define.  So, don't make a claim about it.
>
> In Python 2.7 type(type) is type (not object), but isinstance(type, object) is true -- so it is ambiguous, even contradictory, in the language.
>

I'm not saying this for dreamingforward's benefit, as he clearly isn't
interested in learning, but in case the OP is confused: Checking the
type of something doesn't tell you what it isn't. The isinstance check
is true, because type is an instance of type, which is a subclass of
object, therefore type is an instance of object. Similarly, you could
check this:

>>> issubclass(type, object)
True

So you can see that a type IS an object.

ChrisA

Reply | Threaded
Open this post in threaded view
|

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

Rustom Mody
In reply to this post by zipher
Two unrelated points:

On Tuesday, June 2, 2015 at 5:54:43 AM UTC+5:30, TheDoctor wrote:

> On Monday, June 1, 2015 at 7:03:49 AM UTC-5, Eddilbert Macharia wrote:
> > I think i kind of understand now.
> >
> > Instead of python having data types like int, string, e.t.c it has two primitive types which are class type and class object which are created by python interpreter during its setup .using this two classes python is able to create some more data types
>
> Eddibert, don't let this crowd (Terry, Stephen, et al.) confuse you.  They are as confused about it as you are.  
>
> The truth is, that they've told a lie to themselves (everything is an object) to hide some details FROM THEMSELVES in their model.  They climb a ladder of ambiguity anytime there's a new problem in their model and call it a "type", or they pull out the "metaclass" term (because no one really knows that that is, so they feel *pretty* safe).
>
> A type is not an object in the same way an instantiated type is an object -- anymore than a blueprint for a building is the building itself.

Point 1

Yes.  You may be onto something here Mark [I assume that's your name].
Some very intelligent people have suggested that the sloppy use of 'is'
causes more misunderstanding than any useful communication.
See http://en.wikipedia.org/wiki/E-Prime.

And in "G?del, the Mind and the Laws of Physics" Roger Penrose draws a picture
of Three worlds and the mysteries that connect them
http://sustainedreaction.yuku.com/topic/4931/Three-Worlds-Three-Mysteries#.VW0b9rzGI8o

1. The mathematical world
2. The mental world
3. The physical world

which means that the 'is' in each is different.

As enumerated here http://blog.languager.org/2015/03/cs-history-0.html
the history and very existence of CS has arisen from grapping with these questions.

Point 2: [unrelated to the content of the OP]
You may wish to consider whether your style and the names you keep assuming
and changing are helping you.  Best I can see many people have begun to killfile
you.

Reply | Threaded
Open this post in threaded view
|

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

Mark Lawrence
On 02/06/2015 04:15, Rustom Mody wrote:

> Two unrelated points:
>
> On Tuesday, June 2, 2015 at 5:54:43 AM UTC+5:30, TheDoctor wrote:
>> On Monday, June 1, 2015 at 7:03:49 AM UTC-5, Eddilbert Macharia wrote:
>>> I think i kind of understand now.
>>>
>>> Instead of python having data types like int, string, e.t.c it has two primitive types which are class type and class object which are created by python interpreter during its setup .using this two classes python is able to create some more data types
>>
>> Eddibert, don't let this crowd (Terry, Stephen, et al.) confuse you.  They are as confused about it as you are.
>>
>> The truth is, that they've told a lie to themselves (everything is an object) to hide some details FROM THEMSELVES in their model.  They climb a ladder of ambiguity anytime there's a new problem in their model and call it a "type", or they pull out the "metaclass" term (because no one really knows that that is, so they feel *pretty* safe).
>>
>> A type is not an object in the same way an instantiated type is an object -- anymore than a blueprint for a building is the building itself.
>
> Point 1
>
> Yes.  You may be onto something here Mark [I assume that's your name].
> Some very intelligent people have suggested that the sloppy use of 'is'
> causes more misunderstanding than any useful communication.
> See http://en.wikipedia.org/wiki/E-Prime.
>

I thought it was our pot smoking hippy back under another name.  he
seems to have more lives than a black cat and a phoenix combined.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence


Reply | Threaded
Open this post in threaded view
|

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

random832@fastmail.us
In reply to this post by zipher
On Mon, Jun 1, 2015, at 20:24, TheDoctor wrote:
> A type is not an object in the same way an instantiated type is an object
> -- anymore than a blueprint for a building is the building itself.

What is an object?

It's something you can call str or repr on. It's something you can
lookup attributes from its internal dictionary and call methods on. It's
something you can compare whether it's == to something else. It's
something you can pass as a parameter to a function

In many languages, you can't do any of these things (or their
equivalents for those languages) to a type. In others, you have to get a
special "class object" that isn't _really_ the same thing as the type
itself, via some special syntax like typeof(Foo) or Foo.class, and can
only work with it through magic methods in a certain module.

In python, you can. Why is this bad or confusing?

12345