type() new style class instance says "class", not "ObjectType"

Magnus Lie Hetland mlh at vier.idi.ntnu.no
Thu Apr 11 23:28:52 EDT 2002


In article <3cb5bd03$1_6 at goliath.newsgroups.com>, Brad Clements wrote:
>Heh.  3 posts lambasting me for using type(), and none of them actually
>answered my question. ;-)

Sorry about that. It is a very important point, though (not to use
type checking). If you give the problem where you are using it, I'm
sure we can suggest a more Pythonic alternative (if you're interested,
of course).

>The point of my post is: why does type(newstyleinstance) return <class
>__main__.x> rather than something like <instance __main__.x>?

Because <class __main__.x> is the type of newstyleinstance, also
called its class (the class named "__main__.x" or simply "x").

See? In Python 2.2 there is no sharp divide between types and classes
anymore.

>aaaanyway.. "type(thing) in [InstanceType]" is what xml_pickle uses to
>figure out how to pickle stuff.

Well, that doesn't make it any better.

>Since xml_pickle dies trying to pickle instances of new style classes, I
>went through and added isinstance(thing,ObjectType) which seems to work.

Hm. How about simply removing the type check and see what happens?
Such a check might have been necessary with old-style classes
(although I think I'd rather check for the presence of a __class__
attribute or something) it might well be that you can pickle
non-instance objects the same way as instances with newstyle classes.
After all, there is no big difference anymore.

>However you also say its wrong, which I agree is the wrong solution.
>
>I just want to be able to xml_pickle any Python object. I don't write this
>code, I just use it!

I see. It seems to rely on the division between instances and
non-instances, which is becoming more and more meaningless. I suggest
submitting a bug report to the author (suggesting a major rewrite...)

>So what's needed is a reliable mechanism for determining that "something" is
>an instance of a class. But in 2.2, looks like everything is an instance.

More or less.

>Fortunately I can just wait for someone smarter than I am to solve this
>problem.
>
>--
>Novell DeveloperNet Sysop #5

--
Magnus Lie Hetland                                  The Anygui Project
http://hetland.org                                  http://anygui.org



More information about the Python-list mailing list