473,416 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Is type object an instance or class?

JH
Hi

I found that a type/class are both a subclass and a instance of base
type "object".

It conflicts to my understanding that:

1.) a type/class object is created from class statement
2.) a instance is created by "calling" a class object.

A object should not be both a class and an instance at the same time.

Further I found out there is a special type call "type" that is a
subclass of base type "object". All other types are instances of this
type. Even base type "object" is an instance of this special type.

What is role of this type "type" in object creation? Could someone
there straighten this concept a little?

For example (Python2.5):
>>issubclass(int, object)
True
>>isinstance(int, object)
True
>>print type(int)
<type 'type'>
>>isinstance(int, type)
True
>>issubclass(int, type)
False
>>issubclass(type, object)
True
>>isinstance(type, object)
True
>>isinstance(object, type)
True
>>>
Feb 27 '07 #1
5 3149
JH wrote:
Hi

I found that a type/class are both a subclass and a instance of base
type "object".

It conflicts to my understanding that:

1.) a type/class object is created from class statement
2.) a instance is created by "calling" a class object.

A object should not be both a class and an instance at the same time.

Further I found out there is a special type call "type" that is a
subclass of base type "object". All other types are instances of this
type. Even base type "object" is an instance of this special type.

What is role of this type "type" in object creation? Could someone
there straighten this concept a little?

For example (Python2.5):

>>>>issubclass(int, object)

True
>>>>isinstance(int, object)

True
>>>>print type(int)

<type 'type'>
>>>>isinstance(int, type)

True
>>>>issubclass(int, type)

False
>>>>issubclass(type, object)

True
>>>>isinstance(type, object)

True
>>>>isinstance(object, type)

True

Here is a primer on the topic:

http://www.python.org/download/releases/2.2/descrintro/

James
Feb 27 '07 #2
On Feb 26, 8:00 pm, "JH" <john....@sovereign.co.nzwrote:
Hi

I found that a type/class are both a subclass and a instance of base
type "object".

It conflicts to my understanding that:

1.) a type/class object is created from class statement
2.) a instance is created by "calling" a class object.

A object should not be both a class and an instance at the same time.

Further I found out there is a special type call "type" that is a
subclass of base type "object". All other types are instances of this
type. Even base type "object" is an instance of this special type.
Yes. Python has a much broader definition of "object" than say, Java
does. Functions are objects. Modules are objects. And most relevant
here, types are objects. So "int" and even "object" are objects, and
they have type "type".

Confusing, I know, but perfectly logical.

Feb 27 '07 #3
On Mon, 26 Feb 2007 18:00:00 -0800, JH wrote:
A object should not be both a class and an instance at the same time.
Why ever not?

A meta-class is a class (or at least a class-like object) whose instances
are themselves classes. Under the hood, Python uses meta-classes
extensively. Most Python developers probably never need to use it, but if
you want your head to explode, read this:

http://www.python.org/doc/essays/metaclasses/

A couple of more gentle introductions are here:
http://www.python.org/doc/essays/met...a-vladimir.txt
http://www.onlamp.com/pub/a/python/2...taclasses.html

Me, I think I'll stick to class factories.

--
Steven D'Aprano
Feb 27 '07 #4
JH wrote:
I found that a type/class are both a subclass and a instance of base
type "object".

It conflicts to my understanding that:

1.) a type/class object is created from class statement
2.) a instance is created by "calling" a class object.

A object should not be both a class and an instance at the same time.
A class should be an instance. Now what?
Further I found out there is a special type call "type" that is a
subclass of base type "object". All other types are instances of this
type. Even base type "object" is an instance of this special type.

What is role of this type "type" in object creation? Could someone
there straighten this concept a little?
The type of a class is called metaclass. The creation of a class is the same
as an instantiation of its metaclass. You can therefore write

class A(object):
answer = 42

as

A = type("A", (object,), dict(answer=42))

So now we know how to make a class from a metaclass, how can we make a
metaclass? The tailbiting answer is that a metaclass is a class, too.

Can you figure out the result of isinstance(type, type)?

Peter
Feb 27 '07 #5
Hi

The article you read at
http://www.cafepy.com/article/python_types_and_objects is really good,
and the most comprehensive one I know about Python's object system. I
recommend that you read it as many times as you need to understand it.
It can be confusing -- just don't give up ;)

I will try to give some answers to your questions. Note that I assume we
are only speaking of new-style classes. Old-style classes are different,
but pretty uninteresting since there is little reason to use them in new
code.

Another note: As far as this stuff in Python is concerened, "type" and
"class" are, to my knowledge, basically just two different names for the
same thing.

JH schrieb:
Hi
http://www.cafepy.com/article/python_types_and_objects/
I found that a type/class are both a subclass and a instance of base
type "object".

It conflicts to my understanding that:

1.) a type/class object is created from class statement
2.) a instance is created by "calling" a class object.
Why? I see no conflict here.
A object should not be both a class and an instance at the same time.
It should.

1) Everything is an object.
2) Every object is an instance of a class.

From this, it logically follows that every class should also be an
instance of a class. Classes are objects, and objects are instances of
classes.
Further I found out there is a special type call "type" that is a
subclass of base type "object". All other types are instances of this
type. Even base type "object" is an instance of this special type.

What is role of this type "type" in object creation? Could someone
there straighten this concept a little?
I) object' is the root of the inheritance hierarchy. All classes inherit
from 'object'. 'object' is its own baseclass. Also, all objects are
instances of 'object'.

II) 'type' is the root of the type hierarchy. All types (i.e. classes)
are subtypes of 'type'. 'type' is its own type.
Because 'type' also is, like everything else, an object, it is an
instance of 'object' (see I). Because it is a type object (a class), it
also is a subclass of 'object' (again, see I).

Objects are created by instantiating their class/type. Classes (also
called "type objects") are usually created by instantiating 'type'.

The object you instantiate to get a class is called that class'
"metaclass". One can just as well call it that class' type.

Unless you specify something else, the metaclass is 'type'. Sometimes it
can be useful to not use the default 'type' as a metaclass, but a class
that inherits from 'type'.

class A(type):
# Custom code here.
pass

class B(object):
__metaclass__ = A

type(B) # A. We overrode the metaclass.
type(type(B)) # 'type'. The default metaclass.

For example (Python2.5):
>>>issubclass(int, object)
True
All classes are subclasses of 'object'. 'int' is a class, so it is a
subclass of 'object'.
>>>isinstance(int, object)
True
All objects are instances of 'object', and 'int' is an object.
>>>print type(int)
<type 'type'>
'type' is the default type for classes (the "metaclass"). In the case of
'int', that default was not changed.
>>>isinstance(int, type)
True
Since all classes are subclasses of 'object' and 'object' is an instance
of 'type', all classes are instances of 'type'.
>>>issubclass(int, type)
False
No reason why it should be. It could be, but it does not have to.
>>>issubclass(type, object)
True
'type' is a class, all classes are subclasses of 'object'.
>>>isinstance(type, object)
True
'type' is an object, all objects are instances of 'object'.
>>>isinstance(object, type)
True
'object' is a class, all classes are instances of 'type'.
Hope this helps ;)

--
René
Feb 27 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brad Clements | last post by:
I need to dynamically create a new type of class at runtime, based on an oldstyle class and a new style class. I'm using Python 2.3.2 My code: def shipment_from_db_instance(db_shipment):...
14
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 =...
3
by: Mike in Paradise | last post by:
I have an application that is being passed objects which could either be an instance or a Type in the case of a Static Class When you do the GetType on the object that was originally a Static...
0
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico...
5
by: lion | last post by:
in .net, if you set annstance-A of a class equal to another instance-B, a pointer will add to B, but if i want to create a copy of B instead of pointer, how to operate? Note:serialization...
3
by: Charles Law | last post by:
I have the following lines Dim t As Type = GetType(MyType) Dim serialiser As New XmlSerializer(t) I want to be able to do the following with a FileStream fs Dim instance As MyType ...
4
by: andychambers2002 | last post by:
I'm working on a "TempFile" class that stores the data in memory until it gets larger than a specified threshold (as per PEP 42). Whilst trying to implement it, I've come across some strange...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
21
by: Nikolaus Rath | last post by:
Hello, Can someone explain to me the difference between a type and a class? After reading http://www.cafepy.com/article/python_types_and_objects/ it seems to me that classes and types are...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.