What is an instance and what isn't?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu May 24 17:27:21 EDT 2007


En Thu, 24 May 2007 12:18:36 -0300, Gre7g Luterman <hafeliel at yahoo.com>  
escribió:

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

PyInstance refers to instances of old style classes, now used much less  
than before.

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

The "instance" type refers to said instances of old-style classes. For new  
style classes, when you call type(somenewstyleclass) you usually get  
`type`, and for its instances, you get somenewstyleclass.

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

What do you mean by "instance-like"? All new objects are instances of its  
class, and all new classes are instances of type (or a custom metaclass).

-- 
Gabriel Genellina




More information about the Python-list mailing list