Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

a couple of questions: pickling objects and strict types

10 views
Skip to first unread message

Littlefield, Tyler

unread,
Apr 5, 2013, 2:59:04 PM4/5/13
to Python
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?

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,

--
Take care,
Ty
http://tds-solutions.net
The aspen project: a barebones light-weight mud engine:
http://code.google.com/p/aspenmud
He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.

John Gordon

unread,
Apr 5, 2013, 3:27:30 PM4/5/13
to
In <mailman.159.1365188...@python.org> "Littlefield, Tyler" <ty...@tysdomain.com> writes:

> 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?

The py_compile module can catch some obvious syntax errors, such as
incorrect indentation levels or a missing colon at the end of an if
statement.

But it won't catch other errors such as using a variable name before it's
defined. For that, you can use an external program such as pylint or
pyflakes.

--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Steven D'Aprano

unread,
Apr 5, 2013, 4:30:18 PM4/5/13
to
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?

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.


> 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?



--
Steven

Littlefield, Tyler

unread,
Apr 5, 2013, 8:18:51 PM4/5/13
to Steven D'Aprano, pytho...@python.org
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.

> 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.

>> 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.

Chris Angelico

unread,
Apr 5, 2013, 8:31:35 PM4/5/13
to pytho...@python.org
On Sat, Apr 6, 2013 at 5:59 AM, Littlefield, Tyler <ty...@tysdomain.com> wrote:
> 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.

Syntax errors you'll still catch just by attempting to load up the
module in any way:

def foo():
if True # oops, no colon
pass

You don't need to execute that particular line of code to get the
error. But there are a whole lot of errors that a C++ compiler would
catch that Python leaves until run-time, such as variable name
misspellings, data type mismatches, etc, etc... however, this is part
of what gives Python its flexibility. In fact, a lot of things that
would be errors in C/C++ are actually quite legal and useful in
Python.

The one area that I would prefer the C model, though, is declared
variables vs freely-usable names. I prefer the Python notion of
"names" rather than "variables" (the difference takes a lot of words
to explain, but it makes VERY good sense - in a high level language),
but I do like the infinite nesting of scopes and simple catching of
misspellings that the declarations give. Effectively, what I'd like to
see is a declaration "local foo" instead of Python's choice of "global
foo" - with the caveat that globals can be read, but not written,
without that declaration. I think it's clearer and cleaner to declare
all locals. But that's a small matter.

ChrisA

Steven D'Aprano

unread,
Apr 5, 2013, 10:37:31 PM4/5/13
to
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

Chris Angelico

unread,
Apr 5, 2013, 10:49:50 PM4/5/13
to pytho...@python.org
On Sat, Apr 6, 2013 at 1:37 PM, Steven D'Aprano
<steve+comp....@pearwood.info> wrote:
> Did you mean "constructor" rather than C T O R ? Perhaps your voice-to-
> text software (if you are using such) misheard you.

Side point: "ctor" is a common abbreviation for "constructor".

ChrisA

Dave Angel

unread,
Apr 5, 2013, 11:22:22 PM4/5/13
to pytho...@python.org
On 04/05/2013 10:49 PM, Chris Angelico wrote:
> On Sat, Apr 6, 2013 at 1:37 PM, Steven D'Aprano
> <steve+comp....@pearwood.info> wrote:
>> Did you mean "constructor" rather than C T O R ? Perhaps your voice-to-
>> text software (if you are using such) misheard you.
>
> Side point: "ctor" is a common abbreviation for "constructor".
>
> ChrisA
>

But neither term applies to the __init__() method, which is an
initializer. The constructor is __new__()


--
DaveA

mblume

unread,
Apr 6, 2013, 6:30:29 AM4/6/13
to
Am Sat, 06 Apr 2013 02:37:31 +0000 schrieb Steven D'Aprano:
>>> [...]
>> 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.
>
>
> 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.
>
> [...]
>
To the OP: Did you really mean
self.events['OnLook'] = teventlet()
as opposed to:
self.events['OnLook'] = teventlet

The first one executes teventlet and then assigns the result of the function to
self.events['OnLook']. The second one assigns the function teventlet to the dict
entry (presumably so that it will be called when the objct detects the 'OnLook'
event). Unless the teventlet() function returns itself a function, an 'OnLook'
event won't do anything useful during the remaining life time of the object, I think.

Regards
Martin




0 new messages