What is an instance and what isn't?

Gre7g Luterman hafeliel at yahoo.com
Thu May 24 11:18:36 EDT 2007


I suppose I was lulled into complacency by how Python makes so many things 
look like classes, but I'm starting to realize that they're not, are they?

I'm writing a C program which handles Python objects in different ways based 
on their type. I do a PyInstance_Check(PyObj) to determine if the PyObj is 
an instance, but it is returning 0 on a lot of stuff that I thought would be 
an instance. So I did the following simple test on three things that look 
like instances:

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] 
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class a: pass
...
>>> type(a())
<type 'instance'>
>>> type(Exception())
<type 'exceptions.Exception'>
>>> class b(dict): pass
...
>>> type(b())
<class '__main__.b'>

I was relieved that a() returns an instance, but I was surprised that 
Exceptions aren't really instances at all. And what's the deal with derving 
a class from a standard type like a dictionary? I thought for sure, that 
would be an instance, but this shows it is a class?!?

Can anyone explain the last one and/or give me a simple test I can do in C 
to determine whether a Python object is "instance-like"?

Many thanks,
Gre7g 





More information about the Python-list mailing list