Copy with __slots__
Christian Tismer
tismer at tismer.com
Wed Sep 18 13:01:13 EDT 2002
Alex Martelli wrote:
> Griebel, Peer wrote:
<snip>
> class withslots(yetanother):
> __slots__ = ('s2',)
> def __copy__(self):
> result = self.__class__.__new__(self.__class__)
> for base in self.__class__.__mro__:
> for s in getattr(base, '__slots__', []):
> setattr(result, s, getattr(self, s))
> return result
>
> the slowdown is even more severe -- to 0.62. Still nothing
> like the 1.04 you pay for NOT having the slots at all, of
> course, but it's starting to undermine performance improvement,
> the only real rationale for having __slots__ at all. Still,
> you pays your money, and you makes your choices.
You can get this to the single slot-level speed by doing
the slot grabbing phase of above once, after the class
has been created. I'd add an all_slots class attribute
with all collected slots and then iterate over it.
I think this is quite safe, since when you are using slots,
you are most probably *not* assuming that the classes
will change dynamically. If so, the all_slots need
to be re-built.
ciao - chris
p.s.: I didn't do any testing at all, but I guess some more
speed can be found by not caching the __slots__ but their
get/set wrappers and then just calling these with the new
instance...
--
Christian Tismer :^) <mailto:tismer at tismer.com>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 pager +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/
More information about the Python-list
mailing list