[Python-bugs-list] [ python-Bugs-751321 ] Classes inheritig from object are not class type.

SourceForge.net noreply@sourceforge.net
Fri, 13 Jun 2003 23:13:52 -0700


Bugs item #751321, was opened at 2003-06-09 15:33
Message generated for change (Comment added) made by loewis
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=751321&group_id=5470

Category: Extension Modules
>Group: Not a Bug
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Roger Wenham (e078120)
Assigned to: Nobody/Anonymous (nobody)
Summary: Classes inheritig from object are not class type. 

Initial Comment:
Isinstance(<classinstance>, types.ClassType) will
return false if the class inherits from object.

Here is a demo:

import types
 
class a:
        def test(self):
                pass
 
class b(object):
        def test(self):
                pass
  
if __name__ == '__main__':
        if isinstance(a, types.ClassType):
                print "a is a class"
        else:
                print "a is not a class"
 
        if isinstance(b, types.ClassType):
                print "b is a class"
        else:
                print "b is not a class"

The output look like this:


roger@linux_lap:~ > python demo.py
a is a class
b is not a class



----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2003-06-14 08:13

Message:
Logged In: YES 
user_id=21627

You *are* missing something. It is not logical that 'foo'
has a type of string, whereas UserString.UserString('foo')
has a type of instance - why doesn't it have UserString as
its type?

In general: It is illogical that for some objects, you use
the type to find out what kind of object it is, and for
other objects, you use __class__, as type() only tells you
"yes, it is an instance" (of course it is an instance;
"instance" and "object" ought to be synonyms). This is known
as the "class/type dichotomy", see

http://www.amk.ca/python/writing/warts.html

The newstyle classes intend to fix this problem, unifying
classes and types. As a result, any object should have a
type() which is identical to its __class__. Unfortunately,
for backwards compatibility, old-style classes need to be
preserved for a foreseeable future, however, it is them who
get it wrong, not the newstyle classes.

If you are pickling objects of any type, you will have to
find a mechanism for unpickling them. Fortunately, the
pickle module provides plenty of hooks to perform proper
unpickling, and automatic pickling/unpickling in most cases.

Closing as not-a-bug.

----------------------------------------------------------------------

Comment By: Roger Wenham (e078120)
Date: 2003-06-11 07:29

Message:
Logged In: YES 
user_id=240941

Maybe I'm missing something, but to me a class is a class
and should allways be a ClassType. For instance if I have
pickled
some objects, when unpickling, how else can I identify that
it was a class that I pickled, and that I can take an
instance of it?

With the old style classes, a class was class type and if i
take an instance, it's type is instance, logical.

With new style classes, an class is type 'type', and an
instance of it is type class.

Is it not logical that a class is a class and an istance is
an instance?  

 





----------------------------------------------------------------------

Comment By: Walter Dörwald (doerwalter)
Date: 2003-06-10 20:07

Message:
Logged In: YES 
user_id=89016

The reason for this is that new style classes have a
different meta class than classic classes. This is
documented in http://www.python.org/2.2.3/descrintro.html:
"""The built-in 'type' is the most common metaclass; it is
the metaclass of all built-in types. Classic classes use a
different metaclass: the type known as types.ClassType."""

Why do you think this is a bug?


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=751321&group_id=5470