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

David Mertz, Ph.D. mertz at gnosis.cx
Thu Apr 11 23:49:45 EDT 2002


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

mlh at vier.idi.ntnu.no (Magnus Lie Hetland) wrote:
|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.
|I see. It seems to rely on the division between instances and
|non-instances, which is becoming more and more meaningless.

Unfortunately, there's more to it than this.  There still is--and
presumably always will be--a difference between "instances" and other
types of things.  For example, a list is a mutable sequence that has
some items in it.  In contrast, an instance object--whether new style or
old style (and w/ or w/o __slots__)--is a thing that has some
attributes.  The basic shape of the data will always be different.  The
type/class unification doesn't change that.

Here's a simple example of xml_pickle at work:

    >>> from gnosis.xml.pickle import dumps
    >>> class C: pass
    ...
    >>> o, o2 = C(), C()
    >>> o.lst = [1,2,3]
    >>> o.obj = o2
    >>> print dumps(o)
    <?xml version="1.0"?>
    <!DOCTYPE PyObject SYSTEM "PyObjects.dtd">
    <PyObject module="__main__" class="C" id="1312268">
    <attr name="lst" type="list" id="1309612" >
      <item type="numeric" value="1" />
      <item type="numeric" value="2" />
      <item type="numeric" value="3" />
    </attr>
    <attr name="obj" type="PyObject" id="1312492"
          module="__main__" class="C">
    </attr>
    </PyObject>

It doesn't make sense to treat an instance-like thing the same way as a
list-like thing.  Or for that matter, a number-like thing needs to be
different also (it is a simple value, not a kind of container).

So somewhere in the logic of xml_pickle (however it might be
refactored), we need to ask the question "Is this attribute list-like or
instance-like (or dictionary-like, or number-like, etc)?"  That question
might not be spelled like:

    if type(item) in (ListType, TupleType):

But it needs to get asked one way or another.

Mind you, I know there are some borderline cases.  In fact, xml_pickle
has a lot more flexibility in dealing with borderline cases in
customizable ways than I have indicated.  I raise a question at this
borderline in another note I just sent to the list, in fact.  For
example, is a new style object that inherits from 'list' list-like or
instance-like?  Or an old style object that inherits from UserList?  Or
a NumPy array that is shaped a lot like a list?

These sorts of questions take some thought to answer, but the basic fact
that collections come in different shapes remains true.

Yours, David...

--
    _/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: Postmodern Enterprises _/_/_/
   _/_/    ~~~~~~~~~~~~~~~~~~~~[mertz at gnosis.cx]~~~~~~~~~~~~~~~~~~~~~  _/_/
  _/_/  The opinions expressed here must be those of my employer...   _/_/
 _/_/_/_/_/_/_/_/_/_/ Surely you don't think that *I* believe them!  _/_/






More information about the Python-list mailing list