By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

What is an instance and what isn't?

P: n/a
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
May 24 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Gre7g Luterman" <ha******@yahoo.comwrote in message
news:HK******************************@bresnan.com. ..
|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"?

Your problem is mixing two different meanings of 'instance', one obsolete.
Everything is an instance of its type/class. However, for old-style
classes, all instances of user defined classes are instances of type
'instance' instead of their class. This is usually not very helpful. New
style classes fix this.
>>class a(object): pass
>>type(a())
<class '__main__.a'>

In 2.5, Exception changed from old-style to new.

Terry Jan Reedy

May 24 '07 #2

P: n/a
Gre7g Luterman wrote:
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.
Python has two class systems: classic classes and new-style classes.
New-style classes were introduced with Python 2.2. Classic classes
remain for backwards compatibility. See section 3.3 "New-style and
classic classes" in the Python 2.5 "Python Reference Manual" for an
introduction.

PyInstance_Check() returns true only if an object is a classic class
instance. Built-in types like list and dict are new-style classes, so
PyInstance_Check() will return false (0) for list and dictionary
instances. It will also return false for instances of Python classes
inheriting from a new-style class.
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?!?
Class "a" is a classic class. It does not inherit from a new-style
class. In Python 2.5 Exception became a new-style class as well. dict is
also a new-style class, as mentioned above.
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"?
It all depends on what is meant by "instance-like". All Python classes
are instances as well. In Python a class is defined by how it behaves
rather than by some special interpreter level flag. So there is no
definitive way to separate objects into "classes" and "instances". The
best I can suggest is testing if an object is a "class-like" classic or
new-style class. The inspect module defines an isclass function as follows:

def isclass(object):
"""Return true if the object is a class.

Class objects provide these attributes:
__doc__ documentation string
__module__ name of module in which this class
was defined"""
return isinstance(object, types.ClassType) or \
hasattr(object, '__bases__')

---
Lenard Lindstrom
<le***@telus.net>
May 24 '07 #3

P: n/a
En Thu, 24 May 2007 12:18:36 -0300, Gre7g Luterman <ha******@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

May 24 '07 #4

P: n/a
In article <HK******************************@bresnan.com>,
"Gre7g Luterman" <ha******@yahoo.comwrote:
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?!?

There are actually two kinds of classes in Python (as of 2.2): new-style
classes and classic classes. What you are calling an instance is an
instance of a classic class. Here is how a new-style class would look:
>>class c(object): pass
....
>>type(c())
<class '__main__.c'>

Actually the new-style classes are a lot more consistent with the
built-in types, and the type hierarchy makes more sense with them, so I
would suggest in general migrating towards new-style classes for all of
your own code.

More information about new-style classes can be found here:
http://docs.python.org/ref/node33.html
http://www.python.org/doc/newstyle.html
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"?
With new-style classes, the type of an instance of that class is the
class itself (type(x) == x.__class__). In your above example, class b
is a subclass of dict, which itself is a new-style class, making b a
new-style class as well. Thus type(b()) is b.

As for a check, it really depends on what you mean by "instance-like".
Are dictionaries "instance-like"? What about strings? Integers?

Dave
May 25 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.