Pickling slotted instances

Andrew McNamara andrewm at object-craft.com.au
Tue Sep 17 02:51:18 CEST 2002


>Or, do you mean, by "instance", an object of a certain type which has
>slots? If so, inst.attr is the same as 
>
>getattr(inst.__class__, 'attr').__get__(inst)
>
>(if not inst.__dict__.has_key('attr')). So 'attr' will be searched in
>the class, using the method resolution order. This happens to return 
>a member descriptor, namely <member 'attr' of `inst.__class` objects>.

Yes, that's what I meant - I'd been struggling to work out where attribute
descriptors resided, and you've just added the missing detail - thanks
(I'd been fiddling with "property()" and not getting the results I
expected).

That certainly makes pickling harder.

>"The bases", for me, would put "proper syntax" before support for
>automatic pickling, e.g. via
>
>class X(object):
>  def foo              # defines a slot foo.
>  def bar[transient]   # defines a transient slot.

I'd agree with this. The __slots__ notation is a bit of a wart, but then
again, it's consistent with several other tricks.

>New-style classes are really the same as traditional types. In the
>past, you could not pickle arbitrary types unless you have special
>machinery (such as copy_reg manipulation); so it is not surprising
>that you can't pickle certain cases of new-style classes
>automatically. Notice that you *can* pickle them - just define the
>__getstate__.

I see your point, but it seems like most of the machinery is already
there.  It's a shame that slotted classes aren't more "complete", because
they are the perfect answer to people who use tuples instead of classes
for performance reasons (they are only slightly slower than a tuple).

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/




More information about the Python-list mailing list