a couple of questions: pickling objects and strict types
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Apr 5 22:37:31 EDT 2013
On Fri, 05 Apr 2013 18:18:51 -0600, Littlefield, Tyler wrote:
> On 4/5/2013 2:30 PM, Steven D'Aprano wrote:
>> On Fri, 05 Apr 2013 12:59:04 -0600, Littlefield, Tyler wrote:
>>
>>> Hello all:
>>> I've been using Python for a while now, but I have one larger problem.
>>> I come from a c++ background; though it doesn't help in catching
>>> runtime errors, being able to compile a program helps catch a lot of
>>> syntax errors. I know about pychecker, which is somewhat useful. Do
>>> people have other methods for handling this?
>>
>> Do you tend to make a lot of syntax errors?
>
> Not a -lot-, but there are things I don't catch sometimes.
As we all do. But fortunately the Python compiler catches syntax errors.
>> Python also catches syntax errors at compile-time. I won't speak for
>> others, but I hardly ever make syntax errors: between Python's simple,
>> surprise-free syntax, and modern, syntax-colouring editors, I find that
>> I rarely make syntax errors.
>
> I am blind, so colorful editors don't really work all that well for me.
Fair point.
>>> Also, I'm depickling objects. Is there a way I can force pickle to
>>> call the object's ctor? I set up events per object, but when it just
>>> deserializes it doesn't set all that up. Thanks,
>> What's the object's ctor? What sort of objects are you dealing with?
>>
>>
>>
> def __init__(self):
> self.events = {}
> self.components = []
> self.contents = []
> self.uid = uuid4().int
> self.events['OnLook'] = teventlet()
>
>
> Basically events don't get initialized like I'd like after I depickle
> objects.
Did you mean "constructor" rather than C T O R ? Perhaps your voice-to-
text software (if you are using such) misheard you.
Correct, by default pickle does not call the __init__ method, it just
reconstructs the instance. Basically, it takes a snapshot of the
instance's internal state (the __dict__) and reconstructs from the
snapshot.
This is explained in the documentation here:
http://docs.python.org/2/library/pickle.html#the-pickle-protocol
You can force the __init__ method to be called in a couple of different
ways. Perhaps this is the most straight-forward. Add a __setstate__
method to your class:
def __setstate__(self, state):
self.__dict__.update(state)
self.events['OnLook'] = teventlet()
--
Steven
More information about the Python-list
mailing list