If you derive from set, list, or dict, PySyck ignores the getstate method in the new class. This makes it more difficult to create a slightly custom data type for two reasons:
- The original data type is not preserved.
- Any customization in the data storage is not obeyed.
I haven't had a chance to examine the source code at this point, but I suspect that the dict/list/set types are checked for (probably via isinstance) before the getstate protocol is checked.
Two examples of the type being lost:
>>> class OrderedSet(set):
... def __iter__(self):
... items = list( set.__iter__(self) )
... items.sort( key=str )
... for item in items:
... yield item
... return
... def __getstate__(self):
... return {'ordered_items': set(self)}
...
>>> x = OrderedSet( ('Apples', 'Bananas', 'Cucumbers') )
>>> x
OrderedSet(['Apples', 'Cucumbers', 'Bananas'])
>>> print syck.dump(x)
--- !set
- Apples
- Bananas
- Cucumbers
>>> class DefaultDict(dict):
... def __init__(self, default, *args, **keyargs):
... self.default = default
... dict.__init__(self, *args, **keyargs)
... def __getstate__(self):
... return {'default': self.default, 'dict': dict(self) }
...
>>> d = DefaultDict(-1, test='Hello!')
>>> d.__getstate__()
{'default': -1, 'dict': {'test': 'Hello!'}}
>>> print syck.dump(d)
---
test: Hello!
>>>
An example of a regular object with the getstate() method being properly called:
>>> class RegularObject(object):
... def __init__(self, name, value):
... self.name, self.value = name, value
... def __getstate__(self):
... return {'sillyName': self.name, 'badValue': self.value}
...
>>> a = RegularObject('Answer To Life', 42)
>>> a.__getstate__()
{'badValue': 42, 'sillyName': 'Answer To Life'}
>>> print syck.dump(a)
--- !python/object:__main__.RegularObject
badValue: 42
sillyName: Answer To Life